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

标签 javascript reactjs redux

当我单击 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/

相关文章:

javascript - sublime text 3如何选择打开哪个浏览器

javascript - 尝试在 React 中渲染 Context Provider,收到错误消息 "Element type is invalid..."

reactjs - Redux store 正在更新,但组件没有重新渲染

reactjs - 使用查询字符串触发 Redux/React Router 中的操作

javascript - 通过部分属性值获取元素

Javascript - 从日期数组中获取 1 个最旧的日期

javascript - 尝试删除 Redux ToDo 应用程序中的状态但不起作用

javascript - React Component Prop 不会更新

javascript日期差异

javascript - ReactJS Socket IO useState 到 JSON 对象