I have a problem when using Redux thunk with the connect() method from react-redux . I have the following code:
function Component(props) { return ( <button onClick={props.callback}>Click here</button> ); } function actionCreator() { console.log('#1'); return dispatch => console.log('#2');
When I create the Container component and click the button, only "# 1" is displayed in the console. Thus, thunk is not executed.
When I explicitly dispatch actionCreator() , it works:
const mapDispatchToProps = dispatch => ({ callback: dispatch => dispatch(actionCreator()) });
Redux docs talk about this: mapDispatchToProps :
If an object is passed, each function within it will be considered the creator of the Redux action
So why mapDispatchToProps not dispatch() my actionCreator() ? I'm new to React, so maybe I don't get it right?
Update
When using bindActionCreators() from redux, it works:
const mapDispatchToProps = dispatch => { return bindActionCreators({ callback: actionCreator }, dispatch); };
Is this the right way to bind actioncreators with connect() ?
source share