React-Redux:找不到mapDispatchToProps方法

时间:2016-07-13 19:00:21

标签: reactjs redux

我的印象是,添加到mapDispatchToProps的方法应该可以被this.props访问。当我调用这些方法" Uncaught TypeError:this.props.onHighlight不是一个函数"被提出来了。 任何人都知道发生了什么?一直在为mapDispatchToProps尝试一堆不同的替代品,但它们都不起作用。

   import React from 'react';
    import { addHighlight, deleteHighlight, selectHighlight } from 'actions/highlight';
    import { connect } from 'react-redux';
    import jquery from 'jquery';
    import { styles } from './styles.scss';

    const mapDispatchToProps = dispatch => {
      return {
        onHighlight: (start, end, selectedText) => {
          dispatch(addHighlight(start, end, selectedText));
        },
        onDeleteHighlight: (source) => {
          dispatch(deleteHighlight(source));
        },
        onSelectHighlight: (source) => {
          dispatch(selectHighlight(source));
        }
      };
    }

    const mapStateToProps = state => {
      return { highlights: state.highlights,
               currentTopic: state.currentTopic,
               selectedHighlight: state.selectedHighlight,};
    }

    ...
    Other code about Highlight object 
    ...

    export default connect(
      mapStateToProps,
      mapDispatchToProps
    )(Highlight);

1 个答案:

答案 0 :(得分:0)

尝试使用bindActionCreators

import { bindActionCreators } from 'redux'

const mapDispatchToProps = dispatch => {
      return bindActionCreators({
        onHighlight: addHighlight,
        onDeleteHighlight: deleteHighlight,
        onSelectHighlight: selectHighlight
      }, dispatch);
    }