1
import React from "react";

class App extends React.Component {
    render() {

        var formula = "<div> hello </div>"
        
        const div = document.createElement("div")
        div.innerHTML = formula

        return (

            <div>

                {div}

            </div>



        );
    }
}

export default App;

I get an error

Error: Objects are not valid as a React child (found: [object HTMLLIElement]). If you meant to render a collection of children, use an array instead.

Basically the string has html elements I want to set inner html to the div constant. Right now yeah its relatively simple I just need to know if this can be done

My original code has something like this littered throughout my code in recursions too

var formula = "<div>"
formula = formula + "something"
formula = formula + "</div>
3
  • You might want to use dangerouslySetInnerHTML Commented Aug 14, 2020 at 6:54
  • Does this answer your question? Rendering raw html with reactjs Commented Aug 14, 2020 at 6:55
  • react is used so that we don't need to manipulate the DOM directly using the getElementById etc like native JS. react has virtual DOM in memory and it will re-render when there is a change in state. If you surely want to manipulate DOM and know what you are doing, use a ref using useRef() hook Commented Aug 14, 2020 at 7:03

4 Answers 4

2

This cannot be done this way in React. A component can only return a ReactElement or JSXElement. If for some reason you absolutely need to set HTML from a string (there are reasons for this), you have to use dangerouslySetInnerHTML which was made for that precise purpose, but exposes potential security vulnerabilities as explained in the documentation.

You can accomplish this as detailed in the official example:

import React from "react";

class App extends React.Component {
 
    createMarkup() {
       return {__html: 'Hello'};
    }

    render() {

        return (

            <div dangerouslySetInnerHTML={createMarkup()}/>

        );
    }
}

export default App;

But again, this is discouraged and I'm curious to know why you can't just return JSX markup as the other answers suggest.

Sign up to request clarification or add additional context in comments.

3 Comments

This worked! My original code is constantly adding elements to formula so idk how to do that in jsx
E.g. var formula = "<div>" formula = formula + "something" formula = formula + "</div>" I'd love to know if there is a simpler way to achieve the same as above `
Please post your full code. There are certainly much simpler ways to do this with React. But before going further I would give a thorough read through of their documentation.
2

You don't need to create a element you can directly use :

   class App extends React.Component {
       render() {

           const formula =<div> hello </div>
           
          
           return (

               <div>

                   {formula}

               </div>



           );
       }
   }

   export default App; 

5 Comments

Not element, it is a JSX.
In my original code this wont work because formula isn't a const its a variable that I constantly add stuff too along the way i.e. `formula = formula + "<div> hello </div>"
Do you mean formula is a string ?
yeah it must be a string im too far gone in this
you can directly render the string in render block right ?
0

Why are you creating div like that? I’m begginer in ReactJs ,but I think you shouldn’t touch / create HTML DOM Elements like that at all or if it is not neccesary.

You can simply just do this .

Let formula = <div> hello </div>

Return(
  <div>
    {formula}
  </div>
)

Comments

0

You can do it with useState.

import {useState} from "react";

const App = () => {

  const [formula, setFormula] = useState("");

  setFormula(<div> 'hello' </div>);


  return(
    <div>
      {formula}
    </div>
  );
}

export default App;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.