0.4.0 • Published 3 years ago

simplified-jsx-to-ast v0.4.0

Weekly downloads
6
License
MIT
Repository
github
Last release
3 years ago

simplified-jsx-to-ast

npm

Mod of original simplified-jsx-to-json package. Converts simplified JSX code into a JSON AST representation, which can be used by React.createElement Supports props values from object argument.

Install

$ npm install simplified-jsx-to-ast

Usage

const jsxToJson = require('simplified-jsx-to-json');

jsxToJson('<Test myProp={true}>My Child</Test>');
//=> {
//     type: "Test",
//     props: { myProp: true },
//     children: [
//       {
//         type: "Fragment",
//         props: {},
//         children: "Test",
//       },
//     ],
//   }

Features

  • <Test />: Self-closing JSX tags
  • <Test myProp="string">: String props
  • <Test myProp={`string`}>: Template props
  • <Test myProp>: True props
  • <Test myProp={false}>: Boolean props
  • <Test myProp={34}>: Number props
  • <Test myProp={3 + 3 + 3}>: Props with arithmetic, comparison or bitwise operators
  • <Test myProp={['Test', true, 34]}>: Arrays (with strings, numbers or booleans)
  • <Test myProp={{ test: 34 }}>: Objects with string keys and string, number or boolean value
  • <>Test</>: Fragments
  • HTML/SVG DOM attributes are converted to correct React equivalent (class -> className)

License

MIT © Dennis Morhardt

Modified by Andrey Lapshov