SelectedTopicPage
组件中目前有两种方法,navigateNext
和render
。我想将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>
);
}
});
答案 0 :(得分:1)
您必须使用this
,因此您的代码应为let topicPageNo = this.state.topicPageNo;
并且您的组件应该有类似
的方法getInitialState:function()
{
return{
topicPageNo:0
}
}