【问题标题】:Redux error when submitting a form: Actions must be plain objects. Instead, the actual type was: 'Promise'提交表单时出现 Redux 错误:Actions must be plain objects。相反,实际类型是:\'Promise\'
【发布时间】:2022-11-21 16:56:40
【问题描述】:

我正在尝试使用 Redux 提交表单,但是,在控制台中收到一条错误消息:未捕获错误:操作必须是普通对象。相反,实际类型是:'Promise'。您可能需要将中间件添加到您的商店设置中以处理调度其他值,例如“redux-thunk”来处理调度功能。

在创建商店时,我已经在使用 thunk 作为我的中间件。这是代码:

const store = createStore(reducers, compose(applyMiddleware(thunk)))

创建帖子操作:

export const createPosts = (post) => async (dispatch)=>{
  try {
    const {data} = await api.createPost(post)

    dispatch({type:'CREATE', payload:data})
  } catch (error) {
        console.log(error.message);

  }
}

【问题讨论】:

    标签: javascript reactjs redux redux-toolkit redux-thunk


    【解决方案1】:

    返回 api 调用并在之后进行调度

    export const createPosts = (post) => (dispatch)=>{
      try {
       return api.createPost(post).then(data => {
         dispatch({type:'CREATE', payload:data}) 
       })
    
      } catch (error) {
            console.log(error.message);
    
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-24
      • 2023-02-02
      • 1970-01-01
      • 2022-10-31
      • 2021-08-26
      • 1970-01-01
      • 2021-03-07
      • 2021-11-05
      • 2011-07-28
      相关资源
      最近更新 更多