I do a Fetch query to the Json Placeholder page, and at first it works perfectly, but after a while (it doesn't always take the same time, sometimes it's a few minutes sometimes seconds) the same errors start to repeat in the console many times (sometimes repeating more than 10000 times), this causes the page to freeze and you have to close the tab.
Mistakes:
- App.js:31 GET https://jsonplaceholder.typicode.com/posts net::ERR_INSUFFICIENT_RESOURCES
- Uncaught (in promise) TypeError: Failed to fetch
Function that performs the query:
jsonPlaceHolder = async() =>{
let cons = await fetch('https://jsonplaceholder.typicode.com/posts')// Consulta de jsonplaceholder
cons = await cons.json() // Transformacion de JSON a JS
this.setState({posts:cons}) //Guarda la array de Posts en el stat
}
I do this in the main component, App.js.
I don't know where you
App.js
're calling thatthis.setState({posts:cons})
, but don't forget that callingsetState
onrender
it can cause React to do infinite renders. Perhaps in your case that is causing the API to be consumed indefinitely.So it's best to update states within lifecycle functions like
componentDidMount()
.That's better explained here: loop in react on update state