brandon.joe
brandon.joe

Reputation: 23

How to parse a string of html to actual jsx code without knowing the contents of the string?

I have a problem, the steam api returns content as html code even though my headers are set to "application/json" as well as the formatting is correct in the url parameters. I've accepted that the link is correct, and Steam just returns html code in the "contents" property. I've sent the length to 300 because I want to return a brief description, and I feel like 300 is a length. As you can see here,

http://api.steampowered.com/ISteamNews/GetNewsForApp/v0002/?appid=72850&count=3&maxlength=300&format=json

inside appnews.newsitems.contents that it's all html. I don't know what's going to be new inside the code. so it's very well possible that the string that is returned is something like <a href=\"https://www.modiph... in the middle of html code. Is there any way to solve with without some insane crazy crazy regex?

Upvotes: 0

Views: 30

Answers (1)

Anshul Bansal
Anshul Bansal

Reputation: 1893

You can make use of dangerouslySetInnerHTML function to set the content of react element to JSX.

From the docs.

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

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

Further reading: https://reactjs.org/docs/dom-elements.html

Upvotes: 0

Related Questions