Suggestion: Update mapBuilders to make it easy to add thunks to a slice
See original GitHub issueI’ve reviewed issues: #629 and #1045 and looks like due to TS complexity support for defining thunks in createSlice
has not been possible.
However, a simpler solution can be adopted which can simplify many common use cases of using thunk in createSlice
.
Adding a function like addAsyncThunk
to builder object will help in setting loading conditions automatically for thunk related actions.
So presently when a user is adding thunk (myThunk) to a slice code looks like:
createSlice({
name: "mySlice",
initialState,
reducers: {},
extraReducers: (builder) => {
builder
.addCase(myThunk.pending, (state) => {state[loadingKey] = true})
.addCase(myThunk.rejected, (state) => {state[loadingKey] = false})
.addCase(myThunk.fulfilled, (state, action) => {state[loadingKey] = false; ...other updates})
}
})
For a slice which has many thunks this becomes very long chain, hard to read and debug.
addAsyncThunk
will simplify most common use cases in which pending/rejected actions only change the loading state.
So the above code will look like:
createSlice({
name: "mySlice",
initialState,
reducers: {},
extraReducers: (builder) => {
builder
.addAsyncThunk(myThunk, loadingKey: string, fulfilledCallback)
}
})
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Define thunk on createSlice · Issue #1045 · reduxjs/redux-toolkit
Suggestion : Define thunk on createSlice #1045 ... Suggestion: Update mapBuilders to make it easy to add thunks to a slice #2316.
Read more >How to use Redux-Thunk with Redux Toolkit's createSlice?
I'm a Redux maintainer and creator of Redux Toolkit. FWIW, nothing about making async calls with Redux changes with Redux Toolkit.
Read more >Writing Logic with Thunks - Redux
A thunk action creator is a function that may have some arguments, ... Thunks (and other Redux middleware) give us a place to...
Read more >Building a Parallax Scroller with Pixi.js: Part 4
Adding Wall Slices to the Game Map. Our Walls class contains an array that will represent our map but it's presently empty. Let's...
Read more >Redux Toolkit (Immer & Thunk) - JavaScript in Plain English
We have our basic store setup, our Slice with some simple state that our App Component is subscribed to and a dispatch method...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
For what it’s worth, this is possible to implement in userspace - if you know the trick:
This will still need a little more massaging around the types (I’m in a hurry), but it should show how you can do that with given tools already.
@markerikson @phryneas Thanks for clarification! I can use a function like you have suggested.