Search code examples
redux-thunkredux-toolkit

React Thunk Resolver reasie error: A computed property name must be of type 'string', 'number', 'symbol', or 'any'


Got this error:

A computed property name must be of type 'string', 'number', 'symbol', or 'any'.

In this line

> [getAuthnUser.fulfilled](state, { payload }) {

Component:

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: {
        [getAuthnUser.fulfilled](state, { payload }) {
            state.resp = payload
        },
    },
})

export default userSlice.reducer

Solution

  • We don't recommend the object notation for extraReducers anymore, especially with TypeScript you should be using the "builder notation".

    As you see, the object notation is not playing well with TypeScript.

        extraReducers: builder => {
            builder.addCase(getAuthnUser.fulfilled, (state, { payload })=> {
                state.resp = payload
            }),
        },