React Redux-我的动作创建者没有将动作传递给减速器(同步)

React Redux- My action creator is not passing actions to reducer (sync)

当我点击 Home 容器中的 DIV 时,我确认调用了 set 函数(我看到控制台日志) teamReducer 函数永远不会被调用。也许 bindActionCreators 应该以不同的方式使用?我怎样才能让我的 action creator 发送 action 到 reducer 来更新 league store?

// teamReducer.js
export function teamReducer(state = initialState, action){
  switch (action.type) {
    case 'SET_TEAM':
      return {
        ...state,
        called: true
      };
    default:
      return state;
  }
};


// reducers/index.js
import { combineReducers } from 'redux';
import { routeReducer } from 'redux-simple-router';
import { teamReducer } from './teamReducer';
const rootReducer = combineReducers({
  routing: routeReducer,
  league: teamReducer,
});
export default rootReducer;


// actions/setTeam.js
export function setTeam(team, position) {
    console.log(team, position);
    return {
      type: 'SET_TEAM',
      team,
      position
    };
  }
}



// Home.js
import React, { PropTypes, Component } from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import {setTeam } from '../../actions/teams';
const mapStateToProps = ({league}) => {
  return {
    called: league.called
  };
};
const mapDispatchToProps = (dispatch) => {
  return bindActionCreators({
    setTeam,
  }, dispatch);
};
@connect(mapStateToProps, mapDispatchToProps)
export class Home extends Component {
  constructor(props) {
    super(props);
  }

  render() {
    const {set} = this.props.setTeam
    return <div onClick={set} />
  }
}

render 函数中的问题。你用错了解构赋值。

render() {
    const {set} = this.props.setTeam;
    return <div onClick={set} />
}

此赋值与以下代码相同:

const set = this.props.setTeam.set;

但是 setTeam 是一个函数,没有 set 属性。正确的代码是:

render() {
    const {setTeam} = this.props;
    return <div onClick={setTeam} />
}