![](/img/trans.png)
[英]Redux returns error 'Actions must be plain objects. Use custom middleware for async actions.' even though middleware is defined
[英]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.