如何渲染在 ReactJS 中存储为字符串的 jsx

How to render jsx stored as a string in ReactJS

我正在尝试使用自定义组件构建一个 React 应用程序。应用程序的输入是来自 API 的 jsx(作为字符串)。 现在我想呈现这个输入 jsx(一个字符串变量),但无法做到这一点。我尝试了 eval() 和 dangerouslySetInnerHTML 选项,但没有成功。 我的 JSX 字符串看起来像

'<div> <MyComponent attr_1={this.state.array["field"]}></MyComponent> </div>'

您好,我发现 JsxParser 是解决我问题的正确方法。 react-jsx-parser 提供从 JSX 字符串解析和渲染 React 组件的能力。