Ever needed to show some data in your React component but never understood how to? Below shows a simple example of how you can do this!
Here is a simple API I created, that I will use as an example for this blog post.
https://dal9zp9401.execute-api.eu-central-1.amazonaws.com/latest/api/info
Axios is a promise based HTTP client.
Head over to your terminal and type yarn add axios
Within your React component you will want to do something like this:
import axios from 'axios';
constructor(props) {
super(props);
this.state = {
name: []
}
}
const URL = `https://dal9zp9401.execute-api.eu-central-1.amazonaws.com/latest/api/info`
componentDidMount() {
const url = `${URL}`;
axios.get(url)
.then(response => this.setState({ info: response.data })
.catch(error => console.log(error))
}
render() {
return(
<div>
{this.state.name.map((name) => <p>{name}</p>)}
</div>
)
}