2

I'm calling an API in ReactJS using axios. The call is successful but the output looks like the following:

Bitcoin has inspired other alternative currencies such as 
<a href="https://www.coingecko.com/en/coins/litecoin">Litecoin</a>, 
<a href="https://www.coingecko.com/en/coins/peercoin">Peercoin</a>

How can I render it as HTML and not as a string ?. Here is my code

import React from 'react';
import axios from 'axios';

class API extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            loading: true,
            data: []
        }
    }

    componentDidMount() {

        axios.get('https://api.coingecko.com/api/v3/coins/bitcoin?localization=false')
            .then(res => {
                const data = res.data;
                console.log(res.data);
                this.setState({ data, loading: false })
            })
    }

    render() {
        return (
           <div>
            {this.state.loading ? <p>Loading..</p> : 
              <p>{this.state.data.description.en}</p>
            }
          </div>
        );
    }
}

export default API;
9
  • Please post what you've tried so far Commented Aug 7, 2019 at 13:56
  • @MosèRaguzzini: OP has posted the code what is tried so far? Commented Aug 7, 2019 at 13:58
  • @huMptyduMpty there is no method that try to parse the response. So the OP has not tried to solve it Commented Aug 7, 2019 at 14:00
  • @MosèRaguzzini: lol, that is the question here! Commented Aug 7, 2019 at 14:01
  • There is no attempt in the code to solve the issue of removing <a /> and render the result. Commented Aug 7, 2019 at 14:04

2 Answers 2

2

See dangerouslySetInnerHTML

 render() {
        return (
           <div>
            {this.state.loading ? <p>Loading..</p> : 
              <div dangerouslySetInnerHTML={{__html:this.state.data.description.en}}></div>
            }
          </div>
        );
    }
Sign up to request clarification or add additional context in comments.

4 Comments

OP asked: 'How can I replace the a href tags that are rendered with just "Litecoin" and "Peercoin".'
of course, when you render the <a href="https://www.coingecko.com/en/coins/litecoin">Litecoin</a> as html it becomes Litecoin. You can post an answer or leave it to the OP to comment on it
Ok, maybe the OP refers to the rendered HTML, instead of raw output
This solution answers the problem perfectly. Thanks humpty dumpty :D
2

You can use dangerouslySetInnerHTML, but as the name suggests, it's dangerous. See the snippet for demo code.

const root = document.getElementById("root");
const App = () => {
  const APIData = `Bitcoin has inspired other alternative currencies such as 
<a href="https://www.coingecko.com/en/coins/litecoin">Litecoin</a>, 
<a href="https://www.coingecko.com/en/coins/peercoin">Peercoin</a>`;

  return (
    <div>
      <div dangerouslySetInnerHTML={{__html: APIData }} />
    </div>
  );
};
ReactDOM.render(<App />, root);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></root>

2 Comments

OP asked: 'How can I replace the a href tags that are rendered with just "Litecoin" and "Peercoin".'
I misunderstood the requirement, as replace the a href tags that are rendered with just "Litecoin" and "Peercoin", in my glossary means remove a href tag before render. You can well form a question and set it with wrong tags too.

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.