Redux 分派在连接的组件中不可用

Redux 分派在连接的组件中不可用,redux,react-redux,Redux,React Redux,我试图从连接到redux的react组件调用dispatch。无论出于何种原因,我无法从此组件调用this.props.dispatch。但是,showPopup操作在组件中工作正常吗 它说,只要有connect(),就应该能够调用this.props.dispatch。我有什么遗漏吗 import React, { Component } from 'react'; import { connect } from 'react-redux'; // Actions import { show

我试图从连接到redux的react组件调用dispatch。无论出于何种原因,我无法从此组件调用
this.props.dispatch
。但是,
showPopup
操作在组件中工作正常吗

它说,只要有
connect()
,就应该能够调用
this.props.dispatch
。我有什么遗漏吗

import React, { Component } from 'react';
import { connect } from 'react-redux';

// Actions
import { showPopup } from '../../../actions';

class Channels extends Component {
    render() {
        console.log(this.props.dispatch); // <- undefined (?)
        return <div>some stuff</div>;
    }
}

export default connect(null, { showPopup })(Channels);
更新2

export default connect(null, (dispatch) => bindActionCreators({ showPopup, dispatch }, dispatch))(Channels);

您的第一个代码应该可以工作,只需将
this.props.dispatch
更改为
this.props.showPopup

如果您将第二个参数传递给
connect
方法,它将把这些操作映射为道具<代码>连接(空,{showPopup,otherAction,someAction})(通道),您可以使用
this.props.showPopup()
this.props.otherAction
this.props.someAction
来分派操作


如果您只是执行
connect()(通道)
然后您需要使用
this.props.dispatch(showPopup(…)
this.props.dispatch(otherAction())
this.props.dispatch(someAction())

,这是因为您将mapDispatchToProps定义为一个对象

他说:

您的组件将不再作为道具接收分派


如果您像在第一个示例中那样执行操作,您只需在组件中调用
this.props.showPopup(…)
,它将实际创建并分派您的操作
是您的操作创建者将采取的任何参数。这大大简化了事情,因为您不需要创建然后手动分派操作。@BalázsÉdes我知道,但我仍然需要
分派
来执行另一个操作;)为什么不写一个动作创作者呢?你想要多少就有多少。@BalázsÉdes问个好问题,我想我可以;)只要在连接时将其添加到对象中即可:
{showPopup,otherActionCreator}
谢谢您的帮助,但是调用
showPopup
并不困难。我需要调度调用导入库中的另一个操作。编辑了我的回答以显示正在调度其他操作。只要它返回一个动作对象,您就可以分派它。
export default connect(null, (dispatch) => bindActionCreators({ showPopup, dispatch }, dispatch))(Channels);