Javascript 如何通过此方法调用该方法(React Native)? renderTopicList(){ //console.log(this.props); //log(this.state.dataSource); 返回( ); } renderTopicListCell(数据){ 返回( 选择主题(数据)} 数据={data}/> ); } 选择主题(数据){ 这个是.props.navigator.push({ 标题:'详细' + (data.replies_count?)(“+data.replies_count.toString()+”条回复)' : ''), 组件:TopicView, 通行证:{ 数据:数据 } }); }
在单元格中选择调用:Javascript 如何通过此方法调用该方法(React Native)? renderTopicList(){ //console.log(this.props); //log(this.state.dataSource); 返回( ); } renderTopicListCell(数据){ 返回( 选择主题(数据)} 数据={data}/> ); } 选择主题(数据){ 这个是.props.navigator.push({ 标题:'详细' + (data.replies_count?)(“+data.replies_count.toString()+”条回复)' : ''), 组件:TopicView, 通行证:{ 数据:数据 } }); },javascript,react-native,Javascript,React Native,在单元格中选择调用: 当我点击单元格时,它给出以下错误:_this3.selectTopic不是一个函数。(在“_this3.selectTopic(数据)”中,“_this3.selectTopic”未定义)您可以通过两种方法解决此问题 1) 在组件构造函数中,可以绑定要传递的函数。例如: renderTopicList(){ // console.log(this.props); //console.log(this.state.dataSource); return
当我点击单元格时,它给出以下错误:_this3.selectTopic不是一个函数。(在“_this3.selectTopic(数据)”中,“_this3.selectTopic”未定义)您可以通过两种方法解决此问题 1) 在组件构造函数中,可以绑定要传递的函数。例如:
renderTopicList(){
// console.log(this.props);
//console.log(this.state.dataSource);
return (
<ListView
style={Style.listView}
ref="listview"
pageSize={15}
dataSource={this.state.dataSource}
renderFooter={this.renderFooter}
renderRow={this.renderTopicListCell}
// onEndReached={this.onEndReached}
automaticallyAdjustContentInsets={false}
showsVerticalScrollIndicator={false}
/>
);
}
renderTopicListCell(data){
return (
<TopicListCell
onSelect = { () => this.selectTopic(data) }
data={data} />
);
}
selectTopic(data){
this.props.navigator.push({
title: '详细' + (data.replies_count ? '(' + data.replies_count.toString() + '条回复)' : ''),
component: TopicView,
passProps: {
data: data
}
});
}
然后,您可以使用传递数据
constructor() {
super();
// Binding the function
this.selectTopic.bind(this);
}
renderTopicListCell(数据){
返回(
);
}
2) 您可以在传递函数时绑定它
renderTopicListCell(data){
return (
<TopicListCell
onSelect={this.selectTopic(data)}
data={data} />
);
}
我更喜欢前一种方法,这样我就可以在一个地方进行所有绑定调用,而且我必须绑定一次。谢谢您的帮助,我想知道为什么不能在这里使用arrow函数?这个in-arrow函数引用类对象?
<TopicListCell onSelect={this.selectTopic.bind(this, data)} data={data} />