如何在反应方法之间进行沟通?

时间:2016-01-20 11:07:51

标签: javascript reactjs

SelectedTopicPage组件中目前有两种方法,navigateNextrender。我想将topicPageNo方法的navigateNext值传递给render方法。在React中推荐的方法是什么?当我试图宣布:

topicPageNo = navigateNext.state.topicPageNo;

它提供了undefined error

var SelectedTopicPage = React.createClass({
    navigateNext: function() {
        let topicPageNo = '2';
        this.setState({topicPageNo : topicPageNo});
    },
    render: function() {
        let topicsID = this.props.topicsID;
        let navigateNext = this.navigateNext;
        let topicPageNo = navigateNext.state.topicPageNo;
        return (
            <div>
                {this.props.topicPages.filter(function(topicPage) {
                    return topicPage.topic_no === topicsID && topicPage.topic_page_no === topicPageNo; // if condition is true, item is not filtered out
                }).map(function (topicPage) {
                    return (
                        <div>
                            <div>
                            <SelectedTopicPageMarkup headline={topicPage.headline} key={topicPage.topic_no}>
                                {topicPage.description}
                            </SelectedTopicPageMarkup> 
                            </div>
                            <div>
                                <NextPrevBtn moveNext={navigateNext}/>
                            </div>
                        </div>
                    );
                })}
            </div>
        );
    }
});

1 个答案:

答案 0 :(得分:1)

您必须使用this,因此您的代码应为let topicPageNo = this.state.topicPageNo;

并且您的组件应该有类似

的方法
getInitialState:function()
    {
      return{
        topicPageNo:0
      }
    }