Home > OS >  How to apply builder notation with redux thunk?
How to apply builder notation with redux thunk?

Time:04-17

I try to use builder notation: https://redux-toolkit.js.org/api/createSlice#the-extrareducers-builder-callback-notation

but got this error:

Expression expected.

here, in last line in extraReducers: },

import { createSlice, createAsyncThunk, PayloadAction } from '@reduxjs/toolkit'
import { AuthnRes } from '../tikexModule/Types'
import axios from 'axios'

const initialState: any = {
    resp: null,
}

const namespace = 'user'

export const getAuthnUser = createAsyncThunk(
    `${namespace}/getAuthnUser`,
    async () => {
        const { data } = await axios({
            method: 'get',
            url: 'me',
            headers: { crossDomain: true },
        })
        return data
    }
)

const userSlice = createSlice({
    name: 'authnUser',
    initialState,
    reducers: {},
    extraReducers: (builder) => {
        builder.addCase(getAuthnUser.fulfilled, (state, { payload }) => {
            state.resp = payload
        }),
    }, // <------- HERE
})

//export const { setAuthnRes } = userSlice.actions
export default userSlice.reducer

What is wrong? I can not figure out.

CodePudding user response:

Try removing the comma after addCase

const userSlice = createSlice({
    name: 'authnUser',
    initialState,
    reducers: {},
    extraReducers: (builder) => {
        builder.addCase(getAuthnUser.fulfilled, (state, { payload }) => {
            state.resp = payload
        }) // <------- HERE
    }, 
})
  • Related