如何解析html到React组件?

Sin*_*ing 15 html javascript compilation content-management-system reactjs

这是我的场景:
1.应用程序请求CMS(内容管理系统)的页面内容.
2. CMS返回"<div>Hi,<SpecialButton color="red">My Button</SpecialButton></div>"
3.应用程序使用内容,使用属性中提供的数据呈现相应的组件.

我无法弄清楚如何以React方式执行第3步,任何建议都表示赞赏.

谢谢@Glenn Reyes,这是一个显示问题的沙箱.

import React from 'react';
import { render } from 'react-dom';

const SpecialButton = ({ children, color }) => (
  <button style={{color}}>{children}</button>
);

const htmlFromCMS = `
<div>Hi, 
  <SpecialButton color="red">My Button</SpecialButton>
</div>`;

const App = () => (
  <div dangerouslySetInnerHTML={{__html: htmlFromCMS}}>
  </div>
);

// expect to be same as
// const App = () => (
//   <div>Hi, 
//     <SpecialButton color="red">My Button</SpecialButton>
//   </div>
// );

render(<App />, document.getElementById('root'));
Run Code Online (Sandbox Code Playgroud)

这是 Vuejs 的现场演示.字符串"<div v-demo-widget></div>"可以视为Vuejs指令并呈现.源代码.

Han*_*ans 21

正如EsterlingAccimeYoutuber在这个答案中指出的,parser如果您不想使用dangerouslySetInnerHTML属性,可以使用 a 。

到现在为止,react-html-parser已经三年没有更新了,所以我去寻找不同的模块。

html-react-parser做同样的工作,但经常维护和更新。

html清理您的-String 以防止攻击应该是一个很好的做法XSSdompurify可以用于此目的。

我将 EsterlingAccimeYoutuber 的代码示例更新为以下内容:

import React from 'react';
import { render } from 'react-dom';
import parse from 'html-react-parser';
import DOMPurify from 'dompurify';

const SpecialButton = ({ children, color }) => (
  <button style={{color}}>{children}</button>
);

const htmlFromCMS = `
<div>Hi, 
  <SpecialButton color="red">My Button</SpecialButton>
</div>`;

const htmlFrom = (htmlString) => {
        const cleanHtmlString = DOMPurify.sanitize(htmlString,
          { USE_PROFILES: { html: true } });
        const html = parse(cleanHtmlString);
        return html;
}

const App = () => (
  <div>
     {htmlFromCMS && htmlFrom(htmlFromCMS)}
  </div>
);


render(<App />, document.getElementById('root'));
Run Code Online (Sandbox Code Playgroud)

受上述原帖启发,特别感谢原作者!

  • 感谢您指出维护良好的库“html-react-parser”而不是“react-html-parser”。 (5认同)

gle*_*yes 18

你可能想要深入了解dangerouslySetInnerHTML.以下是如何从React组件中的字符串呈现HTML的示例:

import React from 'react';
import { render } from 'react-dom';

const htmlString = '<h1>Hello World! </h1>';

const App = () => (
  <div dangerouslySetInnerHTML={{ __html: htmlString }} />
);

render(<App />, document.getElementById('root'));
Run Code Online (Sandbox Code Playgroud)

完整示例:https://codesandbox.io/s/xv40xXQzE

dangerouslySetInnerHTML在这里阅读React文档中的更多信息:https://facebook.github.io/react/docs/dom-elements.html#dangerouslysetinnerhtml

  • 如果 htmlString 包含像 `"&lt;h4&gt;Hello&lt;/h4&gt;&lt;MyReactComponent attr1="foo"&gt;&lt;/MyReactComponent&gt;"` 这样的 ReactComponent 字符串怎么办?`MyReactComponent` 将是 html 字符串,而不是真正的组件 (2认同)

小智 6

您可以使用react-html-parser的情况下,你不希望使用dangerouslySetInnerHTML属性

import React from 'react';
import { render } from 'react-dom';
import ReactHtmlParser from 'react-html-parser';

const SpecialButton = ({ children, color }) => (
  <button style={{color}}>{children}</button>
);

const htmlFromCMS = `
<div>Hi, 
  <SpecialButton color="red">My Button</SpecialButton>
</div>`;

const App = () => (
  <div>
     {ReactHtmlParser(htmlFromCMS)}
  </div>
);


render(<App />, document.getElementById('root'));
Run Code Online (Sandbox Code Playgroud)

快乐编码!

  • @AlekseyYaremenko对于自定义组件,您必须使用“transform”选项:https://github.com/peternewnham/react-html-parser/issues/64#issuecomment-501006825 (5认同)
  • `react-html-parser` 不在后台使用 `dangerouslySetInnerHTML`;它使用 [`htmlparser2`](https://www.npmjs.com/package/htmlparser2)。 (3认同)
  • 该库是否在其后台使用“dangerouslySetInnerHTML”? (2认同)

GPr*_*ost 0

您可以尝试使用ReactDOMserver渲染<MyReactComponent />html您的服务器上,然后将其传递到客户端,您可以在客户端插入所有html通过dangerouslySetInnerHTML.