React - How to pass HTML tags in props?

前端 未结 20 1288
甜味超标
甜味超标 2020-12-07 11:58

I want to be able to pass text with HTML tags, like so:

not working.\" />

Bu

相关标签:
20条回答
  • 2020-12-07 12:44

    Adding to the answer: If you intend to parse and you are already in JSX but have an object with nested properties, a very elegant way is to use parentheses in order to force JSX parsing:

    const TestPage = () => (
      <Fragment>
        <MyComponent property={
        {
          html: (
            <p>This is a <a href='#'>test</a> text!</p>
          )
        }}>
        </MyComponent>
      </Fragment>
    );
    
    0 讨论(0)
  • 2020-12-07 12:46

    Parser from html-react-parser is a good solution. You just have to

    • install it with npm or yarn
    • import Parser from 'html-react-parser';
    • call it with :

      <MyComponent text=Parser("This is <strong>not</strong> working.") />
      

      and it works well.

    0 讨论(0)
提交回复
热议问题