How to pass actions down to the components in redux

时间秒杀一切 提交于 2019-12-04 10:14:34

So from what I understand you are using export default connect(mapStateToProps, mapDispatchToProps)(MainLayout) and trying to call dispatch in MainLayout.

So from the react-redux documentation if you see you can call connect in following ways:-

  • connect()(//Component) --> dispatch would be passed as props
  • connect(mapStateToProps)(//Component) --> dispatch would be passed as props
  • connect(mapStateToProps,mapDispatchToProps)(//Component) --> dispatch would not be passed as props to your component

In scenario 3 the behavior is this way because mapDispatchToProps already has access to dispatch function. So you can bind your function to dispatch in mapDispatchToProps and then pass on this function to your component.

Example

The function you want to dispatch are dispatchMe() and dispatchMe2() which calls an action called dispatchAction. So your mapDispatchToProps would be something like :-

function mapDispatchToProps(dispatch){
  return {
     dispatchMe : () => {
       dispatch(dispatchAction()) 
    },
    dispatchMe2 : () => {
      dispatch(dispatchAction2())
    }
  }
}

Now in your component you can pass dispatchMe and call it when required.

You can read here for more info on this

Since you have used bindActionCreators, redux would dispatch the action whenever you call the action creator i.e displayPersonalInformation in your case.

As @Harkirat Saluja mentioned in his answer, if you have used mapDispatchToProps then your component would not have the dispatch function bound with props.

But still if you want to have dispatch with props (not best practice), then you can have something like,

function mapDispatchToProps(dispatch) {
   return { dispatch, ...actions: bindActionCreators({ actionCreators }, dispatch) }
}
  class MainLayout extends React.component{
      render(){
        console.log(this.props.list);
        return(
            <div>
               <h1>mainlayout</h1>
             </div>
            );
        } 
    }
  function mapStateToProps(state) {
       return {
           list: state.listingReducer
       }
   }
   export default connect(mapStateToProps)(MainLayout);

You cannot access your actions through props because you are assigning your actions to the action attribute. The way you bind your actions to your props you could access your functions through this.props.actions.myFunction. If you want to call your actions directly through props connect your actions to your props like this:

const mapDispatchToProps = (dispatch) => {
    return bindActionCreators(actionCreators, dispatch);
};

export default connect(null, mapDispatchToProps)(MainLayout);
import { bindActionCreators, } from 'redux';
import { connect, } from 'react-redux';

/**
 * Map redux actions to the Component's props
 * @param {*} dispatch The Redux store's dispatch method
 * @returns {*} Object
 */
const mapDispatchToProps = (dispatch) => {
    return bindActionCreators({
        yourActions,
    }, dispatch);
};

export default connect(mapDispatchToProps)(MainLayout);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!