If I want to dispatch an action whenever a piece of state changes, currently how I do this is by listening to that state inside a component useEffect hook and dispatching an action whenever that state changes.
For example, let's say I have the following slice:
export const updateBar = createAsyncThunk("mySlice/updateBar", async () => { const { data } = await axios.get("/some/api"); return data; }); const mySlice = createSlice({ name: "mySlice", initialState: { foo: false, bar: 0, }, reducers: { updateFoo: (state, { payload }) => { state.foo = payload; }, }, extraReducers: { [updateBar.fulfilled]: (state, { payload }) => { state.bar = payload; }, }, }); Now if I want to update bar whenever foo changes, I have to go to the component side to add the following code:
const Component = () => { const foo = useSelector((state) => state.mySlice.foo); const dispatch = useDispatch(); useEffect(() => { dispatch(updateBar()); }, [dispatch, foo]); return <div></div>; }; I am wondering is it possible to call updateBar whenever foo changes within the redux slice and without having to touch the component side, because I think that it would be cleaner if all state side effects are abstracted away from the component.