I am having an issue with the useEffect
hook where I would only like a conditional statement inside the callback to execute if, and only if, a specific dependency was changed. For example:
const [index, setIndex] = React.useState(3);
const [dep, setDep] = React.useState(1);
React.useEffect(() => {
if (index === 0) {
if (dep triggered the callback and not index) {
setIndex(dep);
}
}
}, [index, dep]);
setIndex(0); // I do not want `setIndex(dep)` to execute yet
setDep(5); // Now, I want `setIndex(dep)` to execute
How would I achieve this logic in React? Do I need to use a different hook?