向函数发送参数返回无法读取undefined的属性'props'

时间:2017-01-04 05:05:44

标签: javascript reactjs redux react-router react-redux

我是新手做出反应和减少。我试图通过反应路由器发送一个参数,并在一个组件中接收它,该组件负责管理reducer和保持和更新状态的操作。这是我的课程,我的问题出现在那里:

import React from "react";
import {connect} from "react-redux";

import  {getStoriesAction2} from '../../actions/StoryBoardAction2';
import {StoryBoard} from '../composers/story-board/StoryBoard';
class StoryBoardStateManager2 extends React.Component {
render() {
    return (
        <div>
            <StoryBoard storyId={this.props.params.id} stories={this.props.stories2} getStories={()=> this.props.getStories2()}/>
        </div>


    );
}
}

 const mapStateToProps = (state) => {
 return {
    stories2: state.storyBoardReducer2
};
};

 const mapDispatchToProps = (dispatch) => {
  return {
    getStories2:() => {
        dispatch(getStoriesAction2());

    }
  };
 };

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

在上面的storyId = {this.props.params.id}获取通过路由器发送的参数完全正常,但我需要将此id作为参数发送到此函数:getStoriesAction2所以我需要更改函数以下内容:

getStoriesAction2(this.props.params.id)

一旦我添加上述参数,我就会遇到以下错误:

Uncaught TypeError: Cannot read property 'props' of undefined

基本上说不能读取未定义的参数。那么如何将this.props.params.id传递给上面的函数呢?任何人都可以帮助找到我的问题所在吗?

1 个答案:

答案 0 :(得分:2)

只需以这种方式更改mapDispatchToProp:

 const mapDispatchToProps = (dispatch) => {
  return {
    getStories2:(id) => {
        dispatch(getStoriesAction2(id));

    }
  };
 };

并且您可以更改getStories2绑定:

 getStories={()=> this.props.getStories2(this.props.params.id)}