I am trying to fetch a big json file when the webpage has been loaded and then update react state with that data.
Currently, I have this code
get(url) {
return new Promise((resolve, reject) => {
const req = new XMLHttpRequest();
req.open('GET', url);
req.onload = () => req.status === 200 ? resolve(req.response) : reject(Error(req.statusText));
req.onerror = (e) => reject(Error(`Network Error: ${e}`));
req.send();
});
}
componentDidMount(){
this.get('https://x.com/data/tool.json').then((response) =>{
this.setState({"sections" : response});
console.log(this.state);
}).catch((err) =>{
console.log(err);
});
}
The code sets sections to a sting as shown in the screenshot instead of the actual json object.
How can I initialize state with the fetched json.
JSON.parsethe string ?