question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion: Update mapBuilders to make it easy to add thunks to a slice

See original GitHub issue

I’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:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
phryneascommented, May 12, 2022

For what it’s worth, this is possible to implement in userspace - if you know the trick:

import {ActionReducerMapBuilder, CaseReducer, AsyncThunk} from '@reduxjs/toolkit'
import {AsyncThunkFulfilledActionCreator} from '@reduxjs/toolkit/dist/createAsyncThunk'

function addAsyncThunk<S, Returned>(builder: ActionReducerMapBuilder<S>, thunk: AsyncThunk<Returned, any, {}>, loadingKey: keyof S, fulfilledCallback: CaseReducer<S, AsyncThunkFulfilledActionCreator<Returned, any, {}>>){
  builder.addCase(thunk.fulfilled, (s,a) => {
    s[loadingKey] = true
    fulfilledCallback(s,a)
  })
  builder.addCase(thunk.rejected, (s,a)=> {
    s[loadingKey] = false
  })
  builder.addCase(thunk.pending, (s,a)=> {
    s[loadingKey] = true
  })
}

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.

0reactions
edishucommented, May 13, 2022

@markerikson @phryneas Thanks for clarification! I can use a function like you have suggested.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found