我正在学习 redux 并在设置完所有设置后收到此消息:
Error: Actions must be plain objects. Use custom middleware for async actions.
我阅读了 10 个关于这个错误的不同问题,但没有一个解决方案适合我。这是我在 github 上的项目,以防问题不在以下代码中:https://github.com/CodeNinja1395/Test-task-for-inCode/tree/redux
商店:
import {createStore, applyMiddleware, compose} from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers';
const initialState = {};
const middleware = [thunk];
const store = createStore(
rootReducer,
initialState,
applyMiddleware(thunk)
);
export default store;
Action :
import {FETCH_DATA} from './types';
export const fetchData = () => dispatch => {
fetch('https://raw.githubusercontent.com/CodeNinja1395/Test-task-for-inCode/master/clients.json')
.then(posts =>
dispatch({
type: FETCH_DATA,
payload: posts
})
);
};
最佳答案
您使用旧版本的 redux
- v1,而新版本是 v4。当您升级到 v4 时,它会起作用。
npm install redux@^4.0.0
关于javascript - redux react 错误 : Actions must be plain objects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50514133/