I was working on a function which loads some data to a component artificially ie. recreates a page display based on the uploaded data. I am facing issues with when useEffect gets called. Here is the code:
const funcA = (props) => {
let a = [1, 2, 3];
let b = [2, 3];
setSelected(a);
setParam(b);
return (
<div>...</div>
);
}
const funcB = (props) => {
useEffect(
() => {
setParam([]);
},
[props.selected]
);
return (<div>...</div>);
}
setSelected() and setParam() are actions which modify my redux store.
Now When I change selected using setSelected() in funcA, useEffect is called in funcB which modifies the param data based on selected values.
I thought this should not be an issue as I am calling setParam() after calling setSelected but it seems useEffect is getting executed after setParam().
So long story short, this is the order I want them to execute in:
- setSelected()
- useEffect() (because of change in selected)
- setParam()
but they are executing in this order:
- setSelected()
- setParam()
- useEffect() (because of change in selected)
Any help is appreciated. Thanks!!