webpackHotDevClient.js:138 src\App.js Line 18:6: React Hook useEffect has a missing dependency: ‘countries’. Either include it or remove the dependency array react-hooks/exhaustive-deps
useEffect(() => { console.log('effect') axios .get('https://restcountries.eu/rest/v2/all') .then(response => { console.log('promise fulfilled') console.log(response) setCountries(response.data) // countries.push(response.data) // wrong! you should use setCountries }) }, []) // If the second parameter is an empty array [], then the effect is only run along with the first render of the component. (It just renders the component only once like componentDidMount.) console.log('render', countries.length, 'countries')
Because useEffect is run after the render of the component, where you can find this rule by the prints below:
However, with useState(), it will re-render the component and its state countries will be updated to 250 elements and maintain what it have (re-renders caused by other states won’t affect its value due to the state is somewhat independent?), and because we add the second parameter [] of useEffect, now it is just run after the first render of the component.
And here’s a good article introducing how useEffect and its parameters work.