Skip to content

About

Lightweight util to render JSX without react

Topics

Resources

Stars

42 stars

Watchers

2 watching

Forks

Repository files navigation

JSX-render

test

A tiny library to render JSX as real DOM nodes — or as HTML strings on the server — with the familiar stateless-component style from React, without the weight of React itself.

v2 (this branch): JSX now produces a lightweight vnode instead of eager DOM. Render it with renderClient in the browser or renderServer on the server.

Contents

Quick Start

(no build system, just plain html)

<!-- index.html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>
<script type="text/javascript" src="https://alexrqs.github.io/jsx-render/jsx.js"></script>
<script>
  Babel.registerPreset('jsx-render', {
    presets: [[Babel.availablePresets['es2015']]],
    plugins: [
      Babel.availablePlugins['syntax-jsx'],
      [
        Babel.availablePlugins['transform-react-jsx'],
        {
          pragma: 'jsx.dom',
          pragmaFrag: 'jsx.Fragment',
        },
      ],
    ],
  })
</script>

<script type="text/babel" data-presets="jsx-render">
  const foo = () => <p>Hello world</p>

  document.body.appendChild(jsx.renderClient(foo()))
</script>

How To Install

The required packages are @babel/plugin-syntax-jsx, @babel/plugin-transform-react-jsx and of course jsx-render, additionally you will need @babel/core, webpack or any other way to transpile the code that you prefer.

$ npm install jsx-render @babel/plugin-syntax-jsx @babel/plugin-transform-react-jsx

Getting started

Make sure you have the pragma fn defined and its name is "dom"

// .babelrc
{
  "presets": [["@babel/preset-react", { "pragma": "dom" }]]
}

Now you can create components e.g.

import dom, { renderClient } from 'jsx-render'

const DummyComponent = props => <div>{props.children}</div>

document.body.appendChild(renderClient(<DummyComponent>hey</DummyComponent>))

or Fragments

import dom, { Fragment } from 'jsx-render'
import DummyComponent from './DummyComponent'

const Modal = props => (
  <div>
    <header>Include {props.title}</header>
    <Fragment>
      <div>Body</div>
      <DummyComponent>Copyright</DummyComponent>
    </Fragment>
  </div>
)

Features

  • new Class support with default target: the render() method
  • Render Basic Single Components <div />
  • Conditional Component {condition ? <foo/> : <bar/>}
  • Component with Data Attributes <div data-some="attr">
  • Component with Attributes <img src="foo.jpg">
  • Nested Component ul>li>a
  • Siblings Components ul>li*3
  • Components with classname p.chan
  • Map components & numbers array.map(item => <div>{item}</div>)
  • Fragments
  • Portals
  • SVG
  • Component Props <Custom foo="foo">
  • Component Children <Custom>children</Custom>
  • Component render xlinkHref for SVG sprites
  • dangerouslySetInnerHTML
  • Components withState Redux not included

Fragments

import dom, { Fragment, renderClient } from 'jsx-render'

// Return siblings without direct parent component
const Foo = () => (
  <Fragment>
    <li />
    <li />
  </Fragment>
)
const ul = document.createElement('ul')
ul.appendChild(renderClient(<Foo />))

Portals

import dom, { portalCreator } from 'jsx-render'

// can render the component on a different node than the parentNode
// useful for modals, and if the argument is not a node
// it will render as a direct child of body by default
function Component(node) {
  const Portal = portalCreator(node)

  return (
    <div>
      <Portal>
        <span>uno</span>
      </Portal>
    </div>
  )
}

dangerouslySetInnerHTML

function render() {
  return <div dangerouslySetInnerHTML={{ __html: '<span>StrangerDanger</span>' }} />
}

Server-side rendering

renderServer turns the same components into an HTML string. Everything is HTML-escaped except dangerouslySetInnerHTML, so user data is safe to render.

import dom, { renderServer } from 'jsx-render'

const App = props => <h1 className="title">{props.greeting}</h1>

// express handler
app.get('/', (req, res) => {
  res.send(`<div class="app">${renderServer(<App greeting="hola" />)}</div>`)
})

The output matches DOM outerHTML byte-for-byte, so the client can hydrate by replacing or appending with renderClient over the same tree.

Recipes

Testing

Testing

About

Lightweight util to render JSX without react

Topics

Resources

Stars

42 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages