React js Convert String to HTML

React Js Convert String to HTML:In React.js, you can convert a string to HTML by using the dangerouslySetInnerHTML
property. This property allows you to set the HTML content of a component dynamically. First, create a variable that holds the string you want to convert. Then, use this variable in the component's JSX code, setting the dangerouslySetInnerHTML
property to an object with a __html
key and the string value




Thanks for your feedback!
Your contributions will help us to improve service.
How can I convert a string into HTML elements using React.js?
This React.js code converts a string into HTML. It uses the dangerouslySetInnerHTML
property to render the HTML string as actual HTML elements on the page.
The string is stored in the variable htmlString
, which contains a paragraph element with some bold text. The converted HTML is then displayed within a div
element using the dangerouslySetInnerHTML
attribute.
This allows the string to be rendered as HTML rather than plain text.
React Js Convert String into Html Example
<script type="text/babel">
const { useState } = React;
function App() {
const htmlString = '<p>This is a <strong>bold</strong> text.</p>';
return (
<div className='container'>
<h3>React Js Convert string into Html</h3>
<p>String: {htmlString}</p>
<div dangerouslySetInnerHTML={{ __html: htmlString }}></div>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("app"));
</script>