I need to fetch multiple items (about 6)with different ID's passed via props, names are made up, f.e. headerId, bodyId, footerId
.
I made an useEffect for each one:
useEffect(() => {
const getHeader = async () => {
const header = await api.fetch(props.headerId);
setHeader(header);
}
getHeader();
}, [props.headerId])
What I don't like is that now I have same useEffect
s just with different prop
, could I somehow merge all of them or this is the way I should do it.
Passing multiple properties into array within
useEffect
, like:will call the function if even one of the passed properties have changed. I believe you don't really want to call every async request to API (for new header, new body and so on) even if only one prop has changed.
Using multiple
useEffect
allows you to call only that particular request, that it's prop has changed.