How do you lazy-load slices where its extraReducers
listen to a thunk's payload and the thunk is fulfilled before the slice is injected?
#4933
Replies: 1 comment 3 replies
-
In one sense, there isn't a solution here. If that reducer isn't loaded when the fulfilled action is dispatched, then no, it wasn't around to handle the action (aka the "tree falling in a forest and no one's around to hear it" scenario). If there's some other section of the reducer logic that is also listening for this action and does handle it, you could in theory write logic to grab some value from slice B and dispatch another action when slice A gets loaded. But it's also likely that this was the only slice of state that cared about that action, in which case nothing else saved any values from that action at all. A rarely used alternative might be to use a special middleware that watches for that action, and only passes it onwards to the store + root reducer if the slice has been loaded. If not, it could hold the action in a queue, and then actually re-dispatch it once the slice is loaded. Some of the packages I have listed at https://github.com/markerikson/redux-ecosystem-links/blob/master/middleware-async.md#timeouts-and-delays might give you some inspiration there. Out of curiosity, what are the general behavior steps that lead to the thunk being dispatched, and to the slice getting loaded? |
Beta Was this translation helpful? Give feedback.
-
I have a thunk,
thunkA
, which contacts a REST API. I also have a traditional Redux slice with anextraReducers
where it responds to thethunkA
and its payload when it is fulfilled.When trying to make this slice lazy-loaded, the slice doesn't exist immediately. So when
thunkA
is dispatched while this slice is unavailable, the state in this slice cannot be updated; consequently, when the lazy-loaded slice loads, it will just only have the initial state.Is there a way to resolve this so that the lazy-loaded slice can contain the relevant state updates from the action? Do I just need to dispatch the thunk and make another API call again? Is there a solution without doing another API call?
Beta Was this translation helpful? Give feedback.
All reactions