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

时间:2016-02-29 06:43:37

标签: javascript reactjs redux

当我在Home容器中单击DIV时,我已确认调用了set函数(我看到控制台日志) 从不调用teamReducer函数。也许bindActionCreators应该以不同的方式使用?如何让我的动作创建者向减速器发送动作以更新联盟商店?

// 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} />
  }
}

1 个答案:

答案 0 :(得分:1)

Panel2函数中的问题。您使用解构分配错误。

render

此分配与以下代码相同:

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

但是const set = this.props.setTeam.set; 是一个函数,并没有setTeam属性。正确的代码是:

set