React using dangerous SetInnerHTML to render json with html tags

I am trying to display a json list with html tags in a row in a list as follows jsbin . He works at Jsbin. But in my console, I got the warning below:

warning  Only set one of `children` or `props.dangerouslySetInnerHTML` react/no-danger-with-children

just wondering if there is another way to render a list with a danger of SetInnerHTML to avoid a warning?

const displayList = [
    {
        item: 1,
        text: "<strong>ABC</strong> this should be strong."
    },
    {
        item: 2,
        text: "<a>ABC</a> this should be link."
    },
    {
        item: 3,
        text: "normal text"
    }
];

const App = () => (
    <ul>
        {displayList.map((item, i) => (
            <li key={i}>
                <div dangerouslySetInnerHTML={{
                    __html: item.text
                }}>
                </div>
            </li>
        ))}
    </ul>
);

ReactDOM.render(
    <App />,
    document.getElementById('root')
);
+4
source share
2 answers

The reaction complains about being used dangerouslySetInnerHTMLin conjunction with safe child respondents that occurred when you allowed the div tag to open such a characteristic <div>open and ready for children</div>.

JSX, :

<div dangerouslySetInnerHTML={{__html: item.text}}></div>

div:

<div dangerouslySetInnerHTML={{
       __html: item.text
     }}/>

, jsbin, , , , .

+1

div , :

<li key={i}>
    <div dangerouslySetInnerHTML={{
      __html: item.text
     }} />
</li>
Hide result

this, .

0

Source: https://habr.com/ru/post/1676426/


All Articles