Skip to content Skip to sidebar Skip to footer

React.js: Raw HTML String Does Not Gets Recognized As HTML Elements From Node.js

I am sending a raw HTML from node.js to react. and It was successful. However, when I try to render it, they are render as raw string as opposed to HTML elements This is how I get

Solution 1:

You may be able to leverage dangerouslySetInnerHtml, like so:

render()  {
  return (
    <div>
      <div>
        <div className="App">
          <header className="App-header">
            <img src={logo} className="App-logo" alt="logo" />
            <h1 className="App-title">Welcome to Shopping Center</h1>
          </header>
        </div>

        <div 
          className="row"
          dangerouslySetInnerHTML={{__html: this.state.data}}
        />

      </div>
      <button 
        type="button"
        className="btn btn-primary Bottom-right " 
        onClick={(e) => this.handleSubmit(e)}
      >
        My Cart
      </button>
    </div>
  );
}

Solution 2:

You could just wrap your string in the following, once it has been fetched.

document.createRange().createContextualFragment(yourString);

Post a Comment for "React.js: Raw HTML String Does Not Gets Recognized As HTML Elements From Node.js"