Tos Web Developer provides insights, tutorials, and advice around topics including content strategy, design, Drupal Development, Drupal Custom Module Development, Drupal 8 Custom themes, PHP, Server, Twig, and more

React


I had this problem - I needed to add an HTML string in a React application, coming from a WYSIWYG editor, but simply adding {myString} to the JSX was escaping the HTML.. so the HTML tags were displayed to the user!

Solution: use dangerouslySetInnerHTML

You can use the dangerouslySetInnerHTML attribute on an HTML element to add an HTML string inside its content:


<div dangerouslySetInnerHTML={{ __html: props.house.description }}></div>


 Remember that it’s called dangerously for a reason. HTML is not escaped at all in this case, and it might cause XSS issues.



No comments:

Post a Comment

| Designed And Blog Post by www.toswebdeveloper.com