I am trying to delete items from state variable in useEffect using the props comming to the component. I want to take initial values from state variable and then delete elements in that array and set the state variable again to new value. But When I don't pass the state variable in dependency array it is throwing warning as React Hook useEffect has a missing dependency: 'abc'. Either include it or remove the dependency array and when I pass it, It goes in infinite loop which I think is correct because of useeffect working. This is the useffect I am trying:
const [abc, setAbc] = useState([]);
useEffect(() => {
axios.get(url).then(res => setAbc(res));
}, [props.bbb])
useEffect(() => {
if(props.xyz) {
let aaa = abc.filter(key => key.id === props.xyz);
setAbc(aaa);
}
}, [props.xyz])
Is there something I am missing here? Is there any better way to do this?
Thanks in advance.
setAbc(currentAbc => currentAbc.filter(key => key.id === props.xyz))and not provideabcas a dependency.useEffecthooks that have the same dependency and update the same state? Which do you want to win out and update state?useEffects should be one, I'm reading the intended logic as "On change of XYZ reload complete list and then filter based on XYZ" would need more information to be sure.xyzprop updates. The promise chain of the first will overwrite any filtering the second did since it's asynchronous.