简体   繁体   中英

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

When I click the DIV in Home container, I have confirmed the set function is called (I see the console log) teamReducer function is never called. Maybe bindActionCreators should be used differently? How can i have my action creator send action to reducer to update the 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} />
  }
}

The issue in the render function. You use destructuring assignment wrong.

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

This assignment is the same as in the following code:

const set = this.props.setTeam.set;

But setTeam is a function and doesn't have set property. The correct code is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM