[英]Uncaught Error: Actions must be plain objects. Use custom middleware for async actions. even if I am using compose in redux
I am trying to get some data from api call 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;
The fetch data code is: 获取数据代码是:
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)
})
}
I am getting issue: 我遇到了问题:
Uncaught Error: Actions must be plain objects. Use custom middleware for async actions.
What am I doing wrong? 我究竟做错了什么?
Changing actions to use dispatch solved the above issue: 更改使用调度的操作解决了上述问题:
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.