动作必须是普通对象。使用自定义中间件进行异步操作。还原

rbtmr

我不断收到 redux/typescript 的调度错误。如果有人可以帮助纠正我在这里的设置,那就太好了:

店铺:

import {
  configureStore,
  combineReducers,
  MiddlewareArray,
} from '@reduxjs/toolkit'

import { useDispatch } from 'react-redux'

//import slice as reducer
import userReducer from './features/userSlice'
import heroReducer from './features/heroSlice'
import menuReducer from './features/menuSlice'
import utilsReducer from './features/utilsSlice'
import templatesReducer from './features/templatesSlice'

const rootReducer = combineReducers({
  //combine all reducers
  user: userReducer,
  hero: heroReducer,
  menu: menuReducer,
  utils: utilsReducer,
  templates: templatesReducer,
})


export const store = configureStore({
  reducer: rootReducer,
  middleware: new MiddlewareArray(),
})

export type AppDispatch = typeof store.dispatch
export const useAppDispatch = () => useDispatch<AppDispatch>()

调度发生的地方:

  useEffect(() => {
    const fetchData = () => {
      // TODO
      dispatch(fetchUser()).  <--- error portion
    }
    fetchData()
  }, [])

用户切片:

import { createSlice, createSelector, createAsyncThunk } from '@reduxjs/toolkit'
import type { PayloadAction } from '@reduxjs/toolkit'

import axios from 'axios'

export const fetchUser = createAsyncThunk(
  'user/fetchUser',
  async (thunkAPI) => {
    try {
      const response = await axios.get('/api/user', {
        headers: {
          'Content-Type': 'application/json',
        },
      })
      return response.data
    } catch (error) {
      // return rejectWithValue({ error: error.message })
      return console.log(error.message)
    }
  }
)

const userSlice = createSlice({
  name: 'user',
  initialState: {
    userData: {},
    isSubscribed: false,
  },
  reducers: {
    setUserData: (state: any, action: any) => {
      state.userData = action.payload.userData
    },
    setIsSubscribed: (state: any, action: any) => {
      state.isSubscribed = action.payload.isSubscribed
    },
  },
  extraReducers: (builder) => {
    builder.addCase(fetchUser.fulfilled, (state: any, action: any) => {
      state.userData = action.payload
      state.isSubscribed = action.payload.isSubscribed
    })
  },
})

export const userData = createSelector(
  (state: any) => ({
    userData: state.userData,
    isSubscribed: state.isSubscribed,
  }),
  (state: any) => state.userData
)

export const { setUserData, setIsSubscribed } = userSlice.actions
export default userSlice.reducer

不太确定还需要什么来消除类型错误?

原始错误:

错误:操作必须是普通对象。使用自定义中间件进行异步操作。

调度类型错误(fetchUser())

'Argument of type 'AsyncThunkAction<any, void, {}>' is not assignable to parameter of type 'AnyAction''

npm ls redux 显示:

在此处输入图像描述

弗里

middleware: new MiddlewareArray()从商店中删除了所有中间件 - 虽然通常会包括一些中间件。其中包括redux-thunk,它允许发送 thunk。

将其更改为:

export const store = configureStore({
  reducer: rootReducer,
})

顺便说一句你也不需要combineReducers,所以你可以做

export const store = configureStore({
  reducer: {
    user: userReducer,
    hero: heroReducer,
    menu: menuReducer,
    utils: utilsReducer,
    templates: templatesReducer,
  },
})

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

React Native :动作必须是普通对象。使用自定义中间件进行异步操作

Redux错误:动作必须是普通对象。使用自定义中间件进行异步操作

错误:动作必须是普通对象。使用自定义中间件执行异步操作。

动作必须是普通对象。使用自定义中间件进行异步操作,但我使用的是redux-thunk

如何解决:错误:动作必须是普通对象。使用自定义中间件进行异步操作。

React-Redux:动作必须是普通对象。使用自定义中间件进行异步操作

单元测试:动作必须是普通对象。使用自定义中间件进行异步操作

redux-observable动作必须是普通对象。使用自定义中间件进行异步操作

redux-observable + socket.io:动作必须是普通对象。使用自定义中间件进行异步操作

动作必须是普通对象。使用自定义中间件进行异步操作-React Native Redux

React-Redux-Saga:动作必须是普通对象。使用自定义中间件进行异步操作

React Redux Saga:动作必须是普通对象。使用自定义中间件进行异步操作

动作必须是普通对象。即使使用 thunk 作为中间件,也使用自定义中间件进行异步操作错误

动作必须是普通对象,对异步动作使用自定义中间件

“动作必须是普通对象。对异步动作使用自定义中间件。” 与react / redux

错误:动作必须是普通对象。使用自定义中间件执行异步操作。在React Native中

错误:操作必须是普通对象。使用自定义中间件进行异步操作。 - 即使操作包含方法

RXJS 订阅主题 - 操作必须是普通对象。使用自定义中间件进行异步操作

错误:操作必须是普通对象。使用自定义中间件进行异步操作。React-redux 错误

可能未处理的承诺拒绝:错误:操作必须是普通对象。使用自定义中间件进行异步操作

React/Redux... 错误:操作必须是普通对象。使用自定义中间件进行异步操作

Redux错误操作必须是普通对象。使用自定义中间件进行异步操作

我收到此错误:操作必须是普通对象。使用自定义中间件进行异步操作

react-redux错误:操作必须是普通对象。使用自定义中间件进行异步操作

React/Redux:错误:操作必须是普通对象。使用自定义中间件进行异步操作

Redux中间件问题-错误:操作必须是纯对象。使用自定义中间件进行异步操作

使用Jest模拟会导致“动作必须是普通对象。将自定义中间件用于异步动作”。

错误:动作必须是普通对象。使用自定义中间件执行异步操作。但是我没有异步功能

为什么我收到“错误:操作必须是普通对象。对异步操作使用自定义中间件。” 错误?