这是我的组件UserHandler
的代码
import React, { Component } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import * as actionCreators from '../../store/actions/actions';
class UserHandler extends Component {
componentDidMount() {
const { onInitUsers } = this.props;
onInitUsers();
}
render() {
// some code
return (
{/* SOME JSX */}
);
}
}
const mapStateToProps = state => ({
// state to props mapping
});
const mapDispatchToProps = dispatch => ({
onUserEdition: username => dispatch(actionCreators.editUser(username)),
onUserSelection: username => dispatch(actionCreators.fetchSelectedUser(username)),
onInitUsers: () => dispatch(actionCreators.initUsers()),
});
// PropTypes definition and defaultProps
export default connect(mapStateToProps, mapDispatchToProps)(UserHandler);
在我的 actionCreators
文件中,我定义了以下内容:
const initUsersStart = createAction(INIT_USERS_START);
const setUsers = createAction(SET_USERS, users => ({ users }));
const initUsersError = createAction(INIT_USERS_ERROR, error => ({ error }));
const initUsers = () => (dispatch) => {
dispatch(initUsersStart());
return axios.get('/users')
.then(
response => dispatch(setUsers(response.data)),
)
.catch(
error => dispatch(initUsersError(error)),
);
};
当我在 componentDidMount()
函数中注释 onInitUsers();
调用时,我不再遇到错误。我想了解为什么会引发此错误。
最后,这是我如何创建我的商店
:
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import { createStore, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import thunk from 'redux-thunk';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import '../node_modules/bootstrap/dist/css/bootstrap.min.css';
import reducer from './store/reducers/reducers';
const store = createStore(
reducer, /* preloadedState, */
applyMiddleware(thunk),
/* eslint-disable no-undef */ /* eslint-disable no-underscore-dangle */
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__(),
/* eslint-enable */
);
// const store = createStore(reducer);
ReactDOM.render(
<Provider store={store}>
<BrowserRouter>
<App />
</BrowserRouter>
</Provider>,
/* eslint-disable no-undef */
document.getElementById('root'),
/* eslint-enable */
);
registerServiceWorker();
请注意,我之前做过一些研究,以找出导致此类错误的根本原因,并且似乎可能有多种原因造成。我检查了我找到的那些,但没有帮助。
最佳答案
返回您的 axios 调用,因此无论您的逻辑遵循哪个路径,Redux 都会根据需要返回一个对象。
const initUsers = () => (dispatch) => {
dispatch(initUsersStart());
return axios.get('/users')
.then(
response => dispatch(setUsers(response.data)),
)
.catch(
error => dispatch(initUsersError(error)),
);
};
测试您的代码后,我认为问题还在于您的商店配置。中间件应作为 createStore
函数的最后一个参数应用:
const store = createStore(
reducer, /* preloadedState, */
/* eslint-disable no-undef */ /* eslint-disable no-underscore-dangle */
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__(),
/* eslint-enable */
applyMiddleware(thunk),
);
我使用 redux v.4.0.0 和react-redux v.5.0.7 对此进行了测试。
关于javascript - React/Redux : Error: Actions must be plain objects. 使用自定义中间件进行异步操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52046340/