"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Beyond `dangerouslySetInnerHTML`: What are the Safe Alternatives to Rendering HTML in React?

Beyond `dangerouslySetInnerHTML`: What are the Safe Alternatives to Rendering HTML in React?

Published on 2025-02-04
Browse:548

  Beyond `dangerouslySetInnerHTML`: What are the Safe Alternatives to Rendering HTML in React?

Rendering Raw HTML with React

Inquiring whether rendering raw HTML with ReactJS is only possible through the 'dangerouslySetInnerHTML' method, the provided code snippet demonstrates the use of a Markdown converter to convert raw text into HTML. However, the question also expresses a desire for directly rendering complex, class-styled HTML markup.

According to the latest developments in React, safer methods now exist for rendering HTML. Here are four alternatives to consider:

4 Options for Rendering HTML in React

  1. Unicode: Save the file as UTF-8 and set the charset to UTF-8, allowing Unicode characters to be used directly.
  2. Unicode Number: Embed the Unicode number for the desired entity within a JavaScript string.
  3. Mixed Array: Combine strings and JSX elements in an array for rendering.
  4. dangerouslySetInnerHTML: As a last resort, raw HTML can be inserted using 'dangerouslySetInnerHTML', but this method should be used sparingly due to security concerns.
Latest tutorial More>

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