6

I received information from an API which comes with HTML. What happens is, when I try to display the information in the code, it transforms the HTML in a string and doesn't read as real HTML.
I search a lot and all I saw was the method of dangerouslySetInnerHTML but I also saw some reviews and comments about it and I don't want to use it if exists another solution. Also, I tried to use Fragmant but not success.

Below is my render() code:

return (
  <div>
    {models.map(model => (
      <a href="/sofa">
        <div className="Parcelas" key={model.id}>
          <img
            src={"url" + model.image}
            className="ParcImage"
            alt="sofa"
          />
          <h1>Sofá {model.name}</h1>
          <h2>
            1,200<span>€</span>
          </h2>

          <p className="Features">{model.description}</p>

          <button className="Botao">
            <p className="MostraDepois">Ver Detalhes</p>
            <span>+</span>
          </button>
          <img src="../../img/points.svg" className="Decoration" alt="points" />
        </div>
      </a>
    ))}
  </div>
);

Here's an image showing:

I want this in HTML

4
  • Could you give us more information, like how you models object looks like or an example screenshot? Commented Jul 26, 2018 at 9:07
  • @MaddEye I edited the question Commented Jul 26, 2018 at 9:11
  • Did you find a safe solution for this? Some threads have suggested libraries but they don't mention anything about the safety. Commented Oct 9, 2019 at 5:55
  • I've used the solution below that Tholle talked about. Idk about security honestly @Omar Commented Oct 10, 2019 at 14:00

2 Answers 2

9

If you have html in a string, you can use dangerouslySetInnerHTML to render it as html.

return (
  <div>
    {models.map(model => (
      <a href="/sofa">
        <div className="Parcelas" key={model.id}>
          <img
            src={"url" + model.image}
            className="ParcImage"
            alt="sofa"
          />
          <h1>Sofá {model.name}</h1>
          <h2>
            1,200<span>€</span>
          </h2>

          <p
            className="Features"
            dangerouslySetInnerHTML={{ __html: model.description }}
          />

          <button className="Botao">
            <p className="MostraDepois">Ver Detalhes</p>
            <span>+</span>
          </button>
          <img src="../../img/points.svg" className="Decoration" alt="points" />
        </div>
      </a>
    ))}
  </div>
);
Sign up to request clarification or add additional context in comments.

Comments

1

Check if the text you're trying to append to the node is no escaped like this:

model: {
  description: '&lt;h1&gt;Hi there!&lt;/h1&gt;'
}

Instead of this:

model: {
  description: '<h1>Hi there!</h1>'
}

if is escaped you should convert it from your server-side.

if you can't try something like this:

<p className="Features">{model.description.fromCharCode(183)}</p>

Another option is a combination of ReactHtmlParser and unescapingHtml:

import ReactHtmlParser from "react-html-parser";

let model = [
  {
    description: "<h1>Hello There</h1>"
  },
  {
    description: "&lt;h1&gt;Hello There&lt;/h1&gt;"
  }
];

function App() {
  function unescapeHTML(html) {
    var escapeEl = document.createElement("textarea");
    escapeEl.innerHTML = html;
    return escapeEl.textContent;
  }

  return (
    <div className="App">
      {model.map(des => {
        return ReactHtmlParser(unescapeHTML(des.description));
      })}
    </div>
  );
}

2 Comments

Thw HTML comes from the API response, how do I do that model: {}? Also, the fromCharCode(183) says error it's not a function
it's simply a view of your possible model json. I editet the answer with an runable example for a working solution

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.