【问题标题】:I am not able to use mapDispatchToProps in my React + Redux application我无法在我的 React + Redux 应用程序中使用 mapDispatchToProps
【发布时间】:2018-08-07 09:13:51
【问题描述】:

在我的 React + Redux 应用程序中,我正在尝试使用 ma​​pDispatchToProps 实用程序,但是每当我将其放入 connect(mapStateToProps, mapDispatchToProps) 时,它都会给我一个错误提示 Uncaught TypeError: dispatch is not a function at new ReduxApp (ReduxApp.js:42)

这可能是什么问题? PS:下面是文件

ReduxApp.js

 import React from 'react';
 import { Router, Route } from 'react-router-dom';
 import { connect } from 'react-redux';

 import { history } from './_helpers';
 import { alertActions } from './_actions'

 class ReduxApp extends React.Component {

  constructor(props) {
      super(props);

      this.handleChange = this.handleChange.bind(this);
      const { dispatch } = this.props;

      dispatch(alertActions.success("hello world"));
  }

  handleChange(){

      this.props.dispatch(alertActions.clear());
  }

  render(){
     const { alert } = this.props;
     return(

      <div>

         <h1>{alert.message}</h1>
         <button onClick={this.handleChange}>clear</button> {/* this is working since this function is declared outside the mapDispatchToProps. */}
         <button onClick={this.props.handleClick}>clear</button>

      </div>

     );

   }

 }

 const mapStateToProps = (state) => ({ 
   alert : state.alert
 });

 const mapDispatchToProps = (dispatch) => ({
   handleClick: () => dispatch(alertActions.clear())
 });

 const connectedApp = connect(mapStateToProps, mapDispatchToProps)(ReduxApp); // when I add mapDispatchToProps in the connect(), I get thhe issue.
 export { connectedApp as ReduxApp }

【问题讨论】:

    标签: javascript reactjs react-native redux


    【解决方案1】:

    您首先需要传递dispatch,因为它在使用mapDispatchToProps 时不可用(请参阅@gaeron Redux 的创建者的答案:https://github.com/reduxjs/react-redux/issues/255

    const mapDispatchToProps = dispatch => ({
      handleClick: () => alertActions.clear(dispatch),
      dispatch,
    });
    

    更新您的 actionCreator 以在 dispatch 的引用可用时调度该操作:

    alert.clear = dispatch => {
      // your logic
      dispatch(ALERT_CLEAR_ACTION) // or whatever you named your action
    }
    

    在你的组件中:

    handleChange = () => this.props.handleClick();
    

    【讨论】:

    【解决方案2】:

    来自 React Redux Official Documentation

    为什么我的连接组件中没有 this.props.dispatch?

    connect() 函数有两个主要参数,都是可选的。第一个,mapStateToProps,是你提供的一个函数,用于在存储发生变化时从存储中提取数据,并将这些值作为道具传递给你的组件。第二个,mapDispatchToProps,是你提供的一个函数,用来利用 store 的 dispatch 函数,通常是通过创建动作创建器的预绑定版本,一旦调用它们就会自动调度它们的动作。

    如果你在调用 connect() 时没有提供自己的 mapDispatchToProps 函数,React Redux 将提供一个默认版本,它只是简单地将 dispatch 函数作为一个 prop 返回。这意味着如果您确实提供了自己的函数,dispatch 不会自动提供。如果您仍希望它作为道具可用,则需要在 mapDispatchToProps 实现中自己显式返回它。

    mapDispatchToProps 实现中返回dispatch 后问题得到解决

     const mapDispatchToProps = (dispatch) => ({
             handleClick: () => dispatch(alertActions.clear()),
             dispatch, //returning dispatch solves the issue
     });
    

    注意:如果我们使用PropTypes,则无需重新调整mapDispatchToProps

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-02
      • 2019-05-03
      • 1970-01-01
      • 2018-01-18
      • 2020-10-15
      相关资源
      最近更新 更多