My application allows users to enter data in many different places, and each time I use something like this to call an async thunk:
dispatch(handleDataEntry({ key, value })
In some cases the results of the API call inform the application that another datapoint needs to be set client-side, so the downstream extraReducer
in my slice (using createSlice
from @reduxjs/toolkit
) should now re-dispatch the thunk. But this seems to be an anti-pattern, and in any case I couldn't find a way to do this, so I'm storing sideEffectKey
and sideEffectValue
in state and having components in cases of non-nullity dispatch the thunk themselves.
This doesn't seem like a good solution, and in fact since sideEffectKey
and sideEffectValue
stay non-null until the second API call completes and I can clear them, handleDataEntry
gets dispatched multiple times, causing me problems.
What's a better solution? Is there some way to do something like this?
[handleDataEntry.fulfilled.toString()]: (
state: Store,
action: {
payload: {
sideEffectKey: string | null
sideEffectValue: string | null
}
}
) => {
if (action.payload.sideEffectKey) {
dispatch(handleDataEntry({ key: sideEffectKey, value: sideEffectValue }))
}
}