Is it possible to call a reducer function from another reducer function (within the same slice) using Redux Toolkit?

The slice object returned from createSlice includes each of the individual case reducer functions you passed in as slice.caseReducers, to help with situations like this. So, you could do: addModule(state, action) { const { moduleName, renderingData } = action.payload; if (!state.modules[moduleName]) { state.modules[moduleName] = renderingData; } WorkspacesSlice.caseReducers.setActiveModule(state, action); }, Also, reducer functions have no this, … Read more

Redux ToolKit: is it possible to dispatch other actions from the same slice in one action created by createAsyncThunk

Possibly it’s not actual and the question is outdated, but there is thunkAPI as second parameter in payload creator of createAsyncThunk, so it can be used like so export const updateData = createAsyncThunk(‘data/update’, async (params, {dispatch}) => { const result = await sdkClient.update({ params }) dispatch(getData()) return result })

Cannot set getState type to RootState in createAsyncThunk

The createAsyncThunk can have the types defined on the generics: export const unsubscribeMeta = createAsyncThunk<apiUnsubscribeResponse, void, {state: RootState }>( ‘meta/unsubscribe’, async (_, { getState }) => { const { meta } = getState(); const res = await client.post<apiUnsubscribeResponse>( `/meta/unsubscribe/${meta.subscriptionId}` ); return res.data.data; } ); Defining the state will automatically make the getState be aware of … Read more

confusion about `useSelector` and `createSelector` with Redux toolkit

A “selector” is any function that accepts the Redux state tree as an argument, and returns some extracted or derived data. That includes plain functions like you showed. In many cases, you want to memoize the calculation of the results, such as mapping over an array of items, so that it’s not re-calculated unless the … Read more

Can I access state inside a createAsyncThunk w/axios with redux toolkit?

The async function consumes a “payload” argument, and secondly a thunkAPI object that contains a getState method. payloadCreator thunkAPI: an object containing all of the parameters that are normally passed to a Redux thunk function, as well as additional options: dispatch: the Redux store dispatch method getState: the Redux store getState method extra: the “extra … Read more

How do you pass arguments to createAsyncThunk in redux toolkit?

This is what React-Redux says when you are using createAsyncThunk You can only pass one argument to the thunk when you dispatch it. If you need to pass multiple values, pass them in a single object So instead of export const submitPaymentToServer = createAsyncThunk( ‘data/fetchAll’, async ({ name, data }) => { // here you … Read more

what is main difference between redux and redux toolkit, is saga required in redux toolkit? [closed]

Redux used to be great but if you have tried none of them, I would highly recommend using Redux-Toolkit. The only case where I may want you to stick to redux is when you’re using class-based components, where Redux Toolkit does have some boilerplate (like Redux) and you may miss out decent support. However with … Read more

Error: An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft

The issue is the use of an arrow function with no curly braces as the reducer, because that acts as an implicit return statement. So, you’re both mutating state.token, and returning the result of the assignment. Per the Immer docs on returning data, there’s a couple ways to fix this: Adding the void operator in … Read more