How to wrap multi actionCreators into one props?

2020-03-06 06:22发布

I'm getting the following error:

Uncaught TypeError: this.props.dispatch is not a function

Here's my component:

import React from 'react';
import PropTypes from 'prop-types';
import {Link} from 'react-router-dom';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import * as jobTitleSkillsActions from '../../actions/jobTitleSkillsActions';
import SkillList from './SkillList';
import * as userPositionActions from '../../actions/userPositionActions';

class SkillPage extends React.Component {

  componentDidMount() {
    this.props.dispatch(userPositionActions.loadUserPositions());
    var job_title_id = this.props.user_positions[0].job_title_id;    this.props.dispatch(jobTitleSkillsActions.loadJobTitleSkills(job_title_id));
  }

.....

const mapStateToProps = state => {
  return {
    job_title_skills: state.job_title_skills,
    user_positions: state.user_positions
  };
};

function mapDispatchToProps(dispatch) {
  return {
    actions: {
      userPositionActions: bindActionCreators(userPositionActions, dispatch),
      jobTitleSkillsActions: bindActionCreators(jobTitleSkillsActions, dispatch),
    }
  };
}

export default connect(mapStateToProps, mapDispatchToProps)(SkillPage);

What am I doing wrong here?

3条回答
Bombasti
2楼-- · 2020-03-06 07:09

Since you have already used mapDispatchToProps, dispatch wont be available to the component as a prop. Since you have used mapDisptachToProps, the jobs actions will be available as props and you can use them like

componentDidMount() {
    this.props.actions.userPositionActions.loadUserPositions());
    var job_title_id = this.props.user_positions[0].job_title_id;
    this.props.actions.jobTitleSkillsActions.loadJobTitleSkills(job_title_id));
  }

However you can simplify it further like

function mapDispatchToProps(dispatch) {
  return bindActionCreators({userPositionActions, jobTitleSkillsActions}, dispatch)  
}

...


componentDidMount() {
    this.props.userPositionActions.loadUserPositions());
    var job_title_id = this.props.user_positions[0].job_title_id;
    this.props.jobTitleSkillsActions.loadJobTitleSkills(job_title_id));
  }
查看更多
淡お忘
3楼-- · 2020-03-06 07:10

As you have provided mapDispatchToProps to connect function, dispatch is not passed as prop to your component.

Your componentDidMount code should be like this:

componentDidMount() {
  const actions = this.props.actions
  actions.userPositionActions.loadUserPositions()
  var job_title_id = this.props.user_positions[0].job_title_id;
  actions.jobTitleSkillsActions.loadJobTitleSkills(job_title_id)
}
查看更多
贪生不怕死
4楼-- · 2020-03-06 07:15

Per my comment, this would be the correct syntax to bind multiple sub-objects worth of action creators:

function mapDispatchToProps(dispatch) {
    return {
        userPositionActions : bindActionCreators(userPositionActions, dispatch),
        jobTitleSkillsActions: bindActionCreators(jobTitleSkillsActions, dispatch),
    }
}
查看更多
登录 后发表回答