It is being mentioned in the react documentation that every thing that is supposed to be changed needs to be present in the dependency array of the useEffect
hook.
I could make use of // eslint-disable-next-line react-hooks/exhaustive-deps
but this is not the ideal way to do it I think.
But what would you do if you want to trigger a Side Effect only when a certain state changes? Not the other things being used to it?
I have a workaround but that doesn't works if you have multiple side effects listening to unique states.
const [state1, setState1] = useState(1);
const [state2, setState2] = useState(2);
const fetchDataWithState = useCallback(() => {
action.fetchData({
state1,
state2,
})
}, [state1, state2])
// Effect to listen to the changes of state1
useEffect(() => {
// Some random work related to state1
fetchDataWithState()
}), [fetchDataWithState, state1])
// Effect to listen to the changes of state2
useEffect(() => {
// Some random work related to state2
fetchDataWithState()
}), [fetchDataWithState, state2])
The above code doesn't work if there are multiple side effects, each specific for a particular state.
If state1
gets changed, the fetchDataWithState
will have a different reference, so it will lead to execute the callback in the second useEffect
which was supposed to triggered only when the state2
changes.
Or should I use // eslint-disable-next-line react-hooks/exhaustive-deps
by not passing fetchDataWithState
it in the dependency array.