Ad

How To Communicate Between Methods In React?

- 1 answer

I have two methods currently in SelectedTopicPage component which is navigateNext and render. I would like to pass the value of topicPageNo from navigateNext method to render method. What is the recommended way to do that in React? When I try to declare:

topicPageNo = navigateNext.state.topicPageNo;

It gives 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>
        );
    }
});
Ad

Answer

you have to use this so your code should be let topicPageNo = this.state.topicPageNo;

and your component should have a method like

getInitialState:function()
    {
      return{
        topicPageNo:0
      }
    }
Ad
source: stackoverflow.com
Ad