Reputation: 309
I'm creating a blog app in React and Rails and I started using react-quill for users to post articles. However, when a user submits their post, the output is a string of HTML elements.
For Example: <p>Line1</p><br /><p>Line2</p>
How might I go about parsing the HTML string so it can display properly?
Many thanks in advance!
Upvotes: 1
Views: 976
Reputation: 21
Use html-react-parser.
import parse from 'html-react-parser';
and use it to display the text properly.
<div>
{
parse("<p>Line1</p><br /><p>Line2</p>")
}
</div>
Upvotes: 0
Reputation: 112807
You can use dangerouslySetInnerHTML
to render HTML from a string.
Example
function App() {
return (
<div
dangerouslySetInnerHTML={{ __html: "<p>Line1</p><br /><p>Line2</p>" }}
/>
);
}
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>
Upvotes: 2