astring-react-create-element
TypeScript icon, indicating that this package has built-in type declarations

0.0.5 • Public • Published

Generate React.createElement() code from JSX AST with astring

Credits to TroyAlford/react-jsx-parser for parts of the code and thanks to Qard/astring-jsx for inspiration

  1. Why use this plugin instead of astring-jsx or other options ?
    This plugins generates React.createElement that are compatible with React-Native.

Install

npm install astring-react-create-element
# or yarn
yarn add astring-react-create-element

Usage

Example with astring default generator:

import astringReactCreateElement from 'astring-react-create-element';
import * as astring from 'astring';

const formattedCode = astring.generate(ast, {
    generator: astringReactCreateElement(astring.GENERATOR),
});

Example with custom generator:

import astringReactCreateElement from 'astring-react-create-element';
import * as astring from 'astring';

const customGenerator = Object.assign({}, astringReactCreateElement(astring.GENERATOR), {
    JSXFragment: (node, state) => {
        customGenerator['JSXElement'](node, state);
    },
});
const formattedCode = astring.generate(ast, {
    generator: customGenerator,
});

Package Sidebar

Install

npm i astring-react-create-element

Weekly Downloads

13

Version

0.0.5

License

MIT

Unpacked Size

6.46 kB

Total Files

5

Last publish

Collaborators

  • andreitelteu