Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to render html tags that were returned by the server dynamically

In my React project, I make an API call to the server which returns some HTML that I have to render. Below is the API call result:
enter image description here

So this unit_configuration_label field does not properly render. The styling that I applied to that b-tag does not apply. I guess its because the page renders first before I get the results from the server, and then when we get it, it just appends this result as a string, this is what I get on render:
enter image description here

And I need to append that HTML inside this span-tag

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

      <div className="room-types">
        <span>{hotel.unit_configuration_label}</span>
      </div>

>Solution :

use "dangerouslySetInnerHTML":

  const data = "Twin Room with Bath </br><b>Hotel room</b>:2 beds";
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <div
      dangerouslySetInnerHTML={{__html: data}}
    />
    </div>
  );

https://codesandbox.io/s/stoic-hypatia-ljyb02

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading