'Setting state in React useEffect hook
In my app I fetch data from my API after a button is clicked that changes the state of endpoint
. Until the data is fetched I want to display a loading icon. After MUCH testing, I finally got it to work. This is what it looks like:
const [endpoint, setEndpoint] = useState('products');
const [loading, setLoading] = useState(true);
const [data, setData] = useState([]);
useEffect(() => {
setLoading(true);
fetch(`/api/${endpoint}`, {
method: 'GET',
})
.then(res => res.json())
.then(data => {
setData(data);
})
.catch(e => {
setData([]);
})
.finally(() => setLoading(false));
}, [endpoint]);
const onSelect = (option) => {
setEndpoint(option);
}
return (
<>
<Sidebar onSelect={onSelect}/>
<div className="main">
<Banner />
{loading ? 'Loading...' : JSON.stringify(data)}
</div>
</>
);
Even though I get the result that I want, I'm a bit confused as to how it all fits together because I'm new to React. Please correct me If I'm wrong, but this is my understanding:
setEndpoint
triggers a re-render, which causes useEffect
to execute because it has a dependency on endpoint
. Each set state call inside useEffect
also causes a re-render. For instance when setLoading(true)
is called, the screen is re-rendered to show 'Loading...'. Once the state of loading
is set to true
, fetch
gets called. As soon as the promise is resolved, setData(data)
causes another re-render. However, my data isn't displayed on screen until setLoading(false)
is called, re-rendering the screen yet again, displaying my data. So in total, the screen is re-rendered 3 times (right?).
I'm still confused because I was under the impression that hooks like useEffect
are asynchronous so why would it wait for the state to be set before continuing to execute the next lines of code?
Solution 1:[1]
The useEffect
hook callback is completely synchronous. What you are seeing is the asynchronous Promise chain at work.
useEffect(() => {
setLoading(true); // (3)
fetch(`/api/${endpoint}`, { // (4)
method: 'GET',
})
.then(res => res.json())
.then(data => {
setData(data); // (6)
})
.catch(e => {
setData([]); // (6)
})
.finally(() => setLoading(false)); // (7)
}, [endpoint]); // (2)
const onSelect = (option) => {
setEndpoint(option); // (1)
}
...
{loading ? 'Loading...' : JSON.stringify(data)} // (5), (8)
What you explain as your understanding is largely correct.
- An
endpoint
state update is enqueued the callback function completes and the state updates is processed, triggering a rerender. - The
useEffect
hook is called, its dependencies checked. Sinceendpoint
value updated, theuseEffect
callback is invoked. - A
loading
state update is enqueued. The callback function is still running. - A
fetch
request is made.fetch
returns a Promise, and the code starts a Promise chain. This chain is asynchronous. TheuseEffect
hook callback now completes and since there is no more synchronous code to run. Theloading
state update is processed and a rerender is triggered. - The UI conditionally renders the loading indicator.
- Here the Promise chain has either resolved or rejected, a
data
state update is enqueued and a Promise returned in the chain. Thedata
state update is processed and a rerender triggered. - The end of the Promise chain, a
loading
state update is enqueued. Theloading
state is processed and a rerender is triggered. - The UI now conditionally renders the data.
So in total, the screen is re-rendered 3 times (right?).
By my count the logic above likely triggered 4 rerenders, but React could have potentially rerendered any number of times for just about any other reason. React components rerender when either state or props update, or the parent component rerenders.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |