I use selector (don't have to) to retrieve the nextStep
I dispatch someAction and this can change nextStep that I want to do, however the nextStep doesn't get updated with the following code.
const App = (props) => {
const dispatch = useDispatch()
const nextStep = useSelector(getNextStep)
const handleSubmit = () => {
dispatch(someAction()) // this can change the nextStep
dispatch(gotoStep(nextStep)) // this nextStep is computed before someAction
}
}
export const getNextStep = (state) => {
let {step, stepList} = state.my_reduxer
let index = stepList.indexOf(step) // someAction changes stepList
let next_step = stepList[index + 1]
return next_step
}
How do I get the updated nextStep for gotoStep action?
-- Edit
I can think of something like the following but not sure if this guarantees the correct behavior
const App = (props) => {
const dispatch = useDispatch();
const nextStep = useSelector(getNextStep);
const [done, setDone] = useState(false);
const handleSubmit = () => {
dispatch(someAction()); // this can change the nextStep
setDone(true);
};
useEffect(() => {
if (done) {
dipatch(gotoStep(nextStep)); // nextStep is hopefully updated?
}
}, [done]);
};