I'm trying to fetch the following API response data in ReactJS. I'm not sure how to do it.. Can someone help me with this? Thanks in advance, I really appreaciate any help or suggestions given.
For my final output, i want to eventually loop the each of the response in the array and display them just like the API response except in a table which represents each shop with address and postal details.
API Response
[
{
title: 'Paragon',
address: '290 Orchard Road #B1-03 Singapore 238859',
postal: '238859'
},
{
title: 'BreadTalk IHQ',
address: '30 Tai Seng Street #01-02 Singapore 534013',
postal: '534013'
},
{
title: 'City Square Mall',
address: '180 Kitchener Road #01-10 Singapore 208539',
postal: '208539'
}
]
Code (details.js)
class Crawler extends React.Component {
// Create constructor with props
constructor(props) {
super(props);
// Create a state that takes in the SEEDURL
this.state = {
seedURL: '',
response: null,
error: null
};
}
// The seed url taken from the input
const seedURL = this.state;
// Take the seedURL and send to API using axios
const url = "/api";
// Send data using axios
axios.defaults.headers.post['Content-Type'] ='application/x-www-form-urlencoded';
axios.defaults.headers.post['Access-Control-Allow-Origin'] = '*';
try {
// Axios takes in the url and SeedURL
axios
.post(url, seedURL)
.then((res) => {
this.setState({response: res, error: null})
})
.catch((err) => {
this.setState({error: err, response: null})
});
} catch (e) {
console.log(e);
render() {
return(
// How do i read the response here?
);
}
}