You get a bonus - 1 coin for daily activity. Now you have 1 coin

How to use innerHTML in React? in REACT

Practice



In React you normally do not use innerHTMLdirectly to manage the content of elements. Instead, React encourages a declarative approach to building user interfaces, where you describe what the UI should look like based on the application state, and React takes care of updating the DOM efficiently.

However, there may be certain scenarios in which you need to use innerHTML, for example when integrating with third-party libraries that expect raw HTML content, or when you need to render HTML content received from an external source such as an API.

To use innerHTMLsafely in React, you can follow these steps:

  1. Create a container element . Start by creating an empty container element in which you want to display the HTML content. You can use a div or any other suitable element for this purpose.

  2. Set innerHTML : once you have the container element, you can set its innerHTMLproperty to the HTML content you want to display. You can do this in the componentDidMountlifecycle method if you are using class components, or in the useEffecthook if you are using functional components.

Here is an example of how you can use innerHTMLwith React Hooks in a functional component:

jsx

import React, { useEffect, useRef } from 'react';

const InnerHTMLComponent = () => {
  const containerRef = useRef(null);

  useEffect(() => {
    // Get the raw HTML content from your data source (e.g., an API response)
    const htmlContent = "<p>Hello, <strong>React</strong> using innerHTML!</p>";

    // Set the innerHTML property of the container element
    if (containerRef.current) {
      containerRef.current.innerHTML = htmlContent;
    }
  }, []);

  return <div ref={containerRef} />;
};

export default InnerHTMLComponent;

In this example we use the useRefhook to create a reference to the divcontainer element. Then, in the useEffect hook, we set the innerHTMLproperty of the container element to the HTML content.

So this React attribute is a replacement for using innerHTML in the browser DOM. Just like innerHTML , using this attribute is risky in view of cross-site scripting (XSS) attacks. You simply need to pass an object with __html

as the key and the HTML text as the value.

In this example MyComponent uses the dangerouslySetInnerHTML attribute to set HTML markup:

function  createMarkup () { return { __html :  ' First & middot; Second ' } }

function  MyComponent () { return < div  dangerouslySetInnerHTML = { createMarkup () } /> }

Remember that using innerHTML can lead to security threats such as cross-site scripting (XSS) attacks if you handle user content without proper sanitisation. So be careful and make sure you sanitise any user content before using innerHTML. In addition, try to explore other React components and techniques to meet your UI needs in a safer and more maintainable way.

created: 2020-02-23
updated: 2026-03-09
321



Was this answer useful?
Choose a quick rating so we can improve the next answer for you.
How satisfied are you?


Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Scripting client side JavaScript, jqvery, BackBone"

Terms: Scripting client side JavaScript, jqvery, BackBone