Javascript redux react错误:操作必须是普通对象
我正在学习redux,设置完所有设置后,我收到以下消息:Javascript redux react错误:操作必须是普通对象,javascript,reactjs,redux,Javascript,Reactjs,Redux,我正在学习redux,设置完所有设置后,我收到以下消息: Error: Actions must be plain objects. Use custom middleware for async actions. 我用这个错误读了10个不同的问题,但没有一个解决方案适合我。这是我在github上的项目,以防问题不在以下代码中: 商店: import {createStore, applyMiddleware, compose} from 'redux'; import thunk f
Error: Actions must be plain objects. Use custom middleware for async actions.
我用这个错误读了10个不同的问题,但没有一个解决方案适合我。这是我在github上的项目,以防问题不在以下代码中:
商店:
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;
行动:
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
})
);
};
您没有返回fetch()调用。您必须确保在action creator内部返回fetch调用
export function fetchData() {
return function(dispatch) {
return fetch( ... ).then( ... ).catch( ... )
}
}
export const fetchData = (params) => dispatch => {
dispatch(beginAjaxCall());
return fetch(...).then(response => {
dispatch(someFunction(response));
}).catch(error => {
throw(error);
});
}
export const loadIncidents = (params) => dispatch => {
dispatch(beginAjaxCall());
return IncidentsApi.getIncidents(params).then(incidents => {
dispatch(loadIncidentsSuccess(incidents));
}).catch(error => {
throw(error);
});
}
顶部是ES5,中间是ES6,第三个是我在项目中使用的函数的ES6版本
希望对您有所帮助。您使用的是旧版本的
redux
-v1,而新版本是v4。当您升级到v4时,它可以工作
npm install redux@^4.0.0