0

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
János
  • 32,867
  • 38
  • 193
  • 353

1 Answers1

2

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
        }),
    },
Oreol Noumodong
  • 141
  • 1
  • 1
  • 9
phry
  • 35,762
  • 5
  • 67
  • 81