5 ms·
I mean, onSuccess could change. And as you suggested, creating the extra callback is redundant. const fetchData = () => { setLoading(true); callApi(
by cellar_door 6y ago
I mean, onSuccess could change. And as you suggested, creating the extra callback is redundant.
const fetchData = () => {
setLoading(true);
callApi()
.then((fetchedData) => {
setData(fetchedData);
onSuccess();
})
.catch((err) => setError(err))
.finally(() => setLoading(false));
};
useEffect(() => {
fetchData();
}, []);
becomes
useEffect(() => {
setLoading(true);
callApi()
.then((fetchedData) => {
setData(fetchedData);
onSuccess();
})
.catch((err) => setError(err))
.finally(() => setLoading(false));
}, [onSuccess]);
- tekkk 6y agoSure it could but if the intention of the developer is to run the API call only once per mount, never otherwise, it would be more appropriate to throw an error instead of just adding pointless dependencies. It just seems like a dumb overhead which goes against the actual goals of the developer.