0

I am sending a HTTP GET request from a browser to an external API using React. I'm getting a response containing some HTML and Javascript which I would like to render in my browser.

Here's my code so far:

  const url = getExternalEndpoint()
  fetch(url)
    .then(function(response) {
      return response.text();
    }).then(function(data) {
      console.log(data);
    })

Based on Retrieve data from a ReadableStream object?

So I can see the HTML in the console, but I'm not sure how to render it.

For context, the external server I'm sending the request to is an OpenID Connect server.

1

1 Answer 1

0

So, there is one library called react-html-parser

To install, use the following command

npm install react-html-parser
# or  
yarn add react-html-parser

Here, you can use the state to update the value from API.

    import ReactHtmlParser from 'react-html-parser'; 
     const [html_string, Sethtml_string] = useState('')

    //set value in the  html_string 
      <div> { ReactHtmlParser (html_string) } </div>
Sign up to request clarification or add additional context in comments.

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.