Render Raw HTML in React using Safer Methods
In React, you can now render raw HTML using safer methods, avoiding the use of dangerouslySetInnerHTML. Here are four options:
1. Unicode Encoding
Use Unicode characters to represent HTML entities in a UTF-8 encoded file:
{`First \u00b7 Second`}
2. Unicode Numbers in JSX Strings
Convert HTML entities to Unicode numbers within JSX strings:
{`First ` String.fromCharCode(183) ` Second`}
3. Mixed Array of Strings and JSX Elements
Combine strings and JSX elements to render complex HTML:
{[`First `, ·, ` Second`]}
4. DangerouslySetInnerHTML as Last Resort
Only use dangerouslySetInnerHTML as a last resort, as it introduces potential security vulnerabilities:
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3