首页 > 解决方案 > Redux 说要在行动中使用普通对象。但不知道在哪里使用普通对象

问题描述

我在 SO 中寻找我的答案,但找不到任何合适的答案。所以在这里我带着我的问题去......

在我的 redux 操作创建器中,我正在从中获取 API 调用,isomorphic-unfetch但我Error: Actions must be plain objects. Use custom middleware for async actions每次都收到消息。虽然我dispatch在我的操作中定义了......

我的操作代码是

const exchangeBuy = ({btc, usdt, id}, url) => {
    return (dispatch) => {
        fetch(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify( { btc, usdt, id } )
        }).then(
            r => r.json()
        ).then(
            r => dispatch({
                type: 'EXCHANGE_BUY',
                payload: r //here r return an object from mongoDB
            })
        )
    }
}

调用它的代码也是

submitExchange(e){
    e.preventDefault()
    const btc = e.target.btcamount.value
    const usdt = e.target.usdprice.value
    this.props.exchangeBuy( //Here it is 
        {
            btc: btc, 
            usdt: usdt, 
            id: this.props.users.id
        }, 
        this.props.apiurl )
}

标签: react-reduxisomorphic-fetch-api

解决方案


推荐阅读