A React declarative component for converting HTML strings into React components. Avoids the use of dangerouslySetInnerHTML and converts standard HTML elements, attributes and inline styles into their React equivalents or Custom Components.
$ npm install react-html-string
or
$ yarn add react-html-stringimportHTMLStringfrom'react-html-string';consthtml=` <div> <h1> Hello from <a href="https://github.com/MomenSherif/react-html-string" target="_blank" >React HTML String</a > </h1> <hr /> <ul> <li>Render HTML string safely</li> <li>Provide <b>Custom Components</b> if needed</li> </ul> <p>Don't forget to ⭐️ the project</p> </div>`;exportdefaultfunctionApp(){return(<div><h1>Hello, React!</h1><HTMLStringhtml={html}/></div>);}importHTMLStringfrom'react-html-string';importHeadingfrom'./components/Heading';constcomponents={a: props=><a{...props}style={{color: 'red'}}/>,h1: Heading,};consthtml=` <div> <h1> Hello from <a href="https://github.com/MomenSherif/react-html-string" target="_blank" >React HTML String</a > </h1> <hr /> <ul> <li>Render HTML string safely</li> <li>Provide <b>Custom Components</b> if needed</li> </ul> <p>Don't forget to ⭐️ the project</p> </div>`;exportdefaultfunctionApp(){return(<div><h1>Hello, React!</h1><HTMLStringhtml={html}components={components}/></div>);}Each component is strongly typed with the equivalent dom node type.
importHTMLString,{Components}from'react-html-string';importHeadingfrom'./components/Heading';constcomponents: Components={a: props=><a{...props}style={{color: 'red'}}/>,h1: Heading,};consthtml=` <div> <h1> Hello from <a href="https://github.com/MomenSherif/react-html-string" target="_blank" >React HTML String</a > </h1> <hr /> <ul> <li>Render HTML string safely</li> <li>Provide <b>Custom Components</b> if needed</li> </ul> <p>Don't forget to ⭐️ the project</p> </div>`;exportdefaultfunctionApp(){return(<div><h1>Hello, React!</h1><HTMLStringhtml={html}components={components}/></div>);}