我正在学习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;
动作:
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
})
);
};
答案 0 :(得分:1)
您使用旧版npm install redux@^4.0.0
- v1,而新版本是v4。当你升级到v4时,它可以工作。
create table b (data timestamp, value XMLTYPE);
答案 1 :(得分:0)
您没有返回您的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版本的函数。
希望有所帮助。