Here is the code of my Component 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);
And in my actionCreators
file, here's what I defined:
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)),
);
};
When I comment the onInitUsers();
call in the componentDidMount()
function, I no more have the error. I want to understand why this is raising this error.
Finally, here's how my I create my store
:
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();
Please note that I have done some research before to find out what may be the root cause of such an error, and it seems that it could emanate from several causes. I checked the ones I found, but it didn't help.
createAction
function?createAction
function is from github.com/pauldijou/redux-act