一个 React 组件,可以解析 JSX 并输出渲染后的 React 组件。
一个 React 组件,可以解析 JSX 并输出渲染后的 React 组件。
import React from 'react'
import JsxParser from 'react-jsx-parser'
import Library from 'some-library-of-components'
class InjectableComponent extends Component {
static defaultProps = {
eventHandler: () => {}
}
// ... inner workings of InjectableComponent
}
const MyComponent = () => (
{ /* ... do stuff ... */ },
}}
components={{ InjectableComponent, Library }}
jsx={`
Header
((data) =>
)
`}
/>
)由于 InjectableComponent 被传递到 JsxParser.props.components 属性中,因此它被视为已知元素。
类型,并在从 JSX 中解析时使用 React.createElement(...) 创建。您还可以传入整个集合
由 Library 绑定显示的组件,然后使用 LibraryName.ComponentName 访问单个项。
最后,关于属性绑定的一些说明。JsxParser 可以处理多种类型的绑定: - 隐式的 true 绑定,例如 ``(相当于 truthyProp={true}) - 字符串值绑定,例如 stringProp="foo" - 表达式绑定,例如 calc={1 + 1} - 名称值绑定,例如 eventHandler={myEventHandler}(请注意,这需要在 bindings 中进行匹配) - 简单的 单语句箭头表达式 (item) => {item.name}
暂无开放 Issues,或尚未同步最近议题。