当我单击 Home 容器中的 DIV 时,我已确认调用了 set 函数(我看到控制台日志) teamReducer 函数从未被调用。也许应该以不同的方式使用bindActionCreators?我如何让我的 Action 创建者将 Action 发送到 reducer 以更新联赛商店?
// 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} />
}
关于javascript - React Redux-我的 Action 创建者没有将 Action 传递给 reducer (同步),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35693415/