【问题标题】:Actions must be plain objects. Use custom middleware for async actions. React native动作必须是普通对象。使用自定义中间件进行异步操作。反应原生
【发布时间】:2020-10-23 02:57:21
【问题描述】:

我有一个函数fundBalance动作函数被调用,在这个函数内部和api调用,在api调用解决之后我调用handleFundBalanceSuccess这个错误发生“动作必须是普通对象。使用自定义中间件进行异步动作。反应原生”

    export function fundBalance(navigation) {
        return async (dispatch) => {
            dispatch({
                type: Fund_Balance_START
            })
            let response = await api.Fund_Balance(),
                responseJson = await response.json();
            if(response.status == 200){
                dispatch(handleFundBalanceSuccess(responseJson, navigation, dispatch)) // this line return Actions must be plain objects. Use custom middleware for async actions
            }
        }
    }

和handleFundBalanceSuccess

handleFundBalanceSuccess = (response, navigation, dispatch) => {
    dispatch({
        type: Fund_Balance_SUCESS,
    })
    navigation.navigate('MessageScreen', {
        title: 'Success',
        description: response.status,
        doneClick: this.fundBalanceOrCashOutSuccessAction.bind(this, navigation),
    })
}

【问题讨论】:

  • 移除 dispatch(handle....) 到 handleFundBalanceSuccess(responseJson, navigation, dispatch)

标签: react-native redux react-redux redux-thunk


【解决方案1】:

对于调度函数,您需要redux-thunk 中间件。 如果没有这个中间件,dispatch 只会期望对象参数。

关于 redux-thunk 的更多细节以及如何实现它,你可以在这里查看 - https://github.com/reduxjs/redux-thunk#motivation

但是,在这种情况下,您不需要调度该函数。只需调用该函数即可解决此问题。

【讨论】:

    【解决方案2】:

    handleFundBalanceSuccess 方法中,您需要返回普通对象。例如,在您的情况下:

    handleFundBalanceSuccess = (response, navigation, dispatch) => {
    . . . . .
    . . . . .
    . . . . .
      return {
       type: Fund_Balance_SUCESS,
      };
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-17
      • 1970-01-01
      • 1970-01-01
      • 2017-09-05
      • 2018-02-19
      • 2018-11-12
      • 2018-01-31
      相关资源
      最近更新 更多