React: Raw HTML tag in content

前端 未结 2 1889
北海茫月
北海茫月 2021-01-22 14:19

I am working on a React app. There is some dynamic content which contents HTML tags. When I am showing that content on the page, it\'s showing with raw HTML tag.

For say

相关标签:
2条回答
  • 2021-01-22 15:18

    You can use the 'dangerouslySetInnerHTML' like:

    const msg = () => ({__html: 'Some <strong>text</strong> here'});
    

    and use it in your code like:

    <div dangerouslySetInnerHTML={createMarkup()} />
    

    For reference for 'dangerouslySetInnerHTML' read this:

    dangerouslySetInnerHTML

    0 讨论(0)
  • 2021-01-22 15:19
    yarn add raw-html-react
    

    or

    npm install raw-html-react
    
    import ReactHtml from 'raw-html-react';
    
    const html = `<div data-react-component="MyComponent"></div>`;
    return <ReactHtml html={html} componentMap={{ MyComponent }} />;
    
    0 讨论(0)
提交回复
热议问题