簡體   English   中英

未捕獲的錯誤:操作必須是普通對象。 使用自定義中間件進行異步操作。 即使我在redux中使用compose

[英]Uncaught Error: Actions must be plain objects. Use custom middleware for async actions. even if I am using compose in redux

我試圖從redux中的api調用中獲取一些數據:

import {createStore,combineReducers,compose,applyMiddleware} from "redux"
import math from "./reducers/mathReducer"
import user from "./reducers/userReducer"
import thunk from "redux-thunk"

const store = createStore(
        combineReducers({
                user,math
            }),
        {},
        compose(applyMiddleware(thunk))
        );
export default store;

獲取數據代碼是:

export function setName()
{
    fetch('http://localhost/login.php', {
        method: 'POST',
        headers: {
            'Accept': 'application/json',
        },
    })
    .then(response => response.json())
    .then( (response) => {
        return {
            type:'SET_NAME',
            payload: response.name
        };
    })
    .catch( (error) => {
        console.warn('Actions - fetchJobs - recreived error: ', error)
    })


}

我遇到了問題:

Uncaught Error: Actions must be plain objects. Use custom middleware for async actions.

我究竟做錯了什么?

更改使用調度的操作解決了上述問題:

export function setName(name)
{
    console.log("Inside setName.");
    return dispatch =>
    {
        fetch('http://localhost/login.php', {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
            },
        })
        .then(response => response.json())
        .then( (response) => {
            dispatch({
                type:'SET_NAME',
                payload: response.name
            });

        })
        .catch( (error) => {
            console.warn('Actions - fetchJobs - recreived error: ', error)
        })

    }   
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM