The question is pretty straight forward. I know that it is supposed to run twice in development to faster identify bugs. But no I ask myself if I need to configure something so it does not happed in production when I deploy my NextJS app.
Here is the issue I am having. So I took some time off programming because of Uni. Now I am trying to fetch some data in useEffect right after render but can't get it to work right.
I could simply do:
useEffect(() => {
fetch(`/api/collection/getCollections`)
.then(res => res.json())
.then(data => setData(data.collections))
.catch(error => console.log(error));
}, [])
Or something like this:
useEffect(() => {
const getData = async () => {
await fetch(`/api/collection/getCollections`).then(res => res.json()).then(data => setData(data.collections)).catch(error => console.log(error));
}
getData();
}, [])
Which is basically the same thing. But both runs twice and I get two API calls
Thank you in advance!
CodePudding user response:
Check out next.config.js
file and make reactStrictMode
equal false
, then restart the node server. This is why your API gets called twice.
React's Strict Mode is a development mode only feature for highlighting potential problems in an application. It helps to identify unsafe lifecycles, legacy API usage, and a number of other features(see here).
So yes, it is only for development mode.