[英]How to use `this` in react-native-collapsible accordion component?
我有一个react-native屏幕,在这里我想使用react-native-colapsible手风琴组件来显示资产列表。 在所需的手风琴的rendercontent
中,我传入了在屏幕组件内部定义的函数sellAsset
在这里我使用this
关键字来引用屏幕组件。 但是它没有用,总是告诉我this.sellAsset is not a function
。 请参见下面的代码。
尝试了一些功能绑定,但是没有用。 似乎传递给手风琴组件的this
并不指向屏幕组件。
那么如何正确调用this.sellAsset
呢?
renderContent(item) {
return (
<View style={[styles.imageContent]}>
<View style={[styles.detailContainer, {paddingVertical: 0}]}>
<Image source={getImage(_.get(item, ['Asset', 'image']))} resizeMode="contain" style={styles.assetImage}/>
</View>
<View style={styles.priceContainer}>
<CustomSignInButton
text="SELL"
onPress={() => {this.sellAsset();}}
buttonBackgroundColor="transparent"
buttonBorderColor="white"
buttonTextColor="white"
buttonHeight={30}
/>
</View>
</View>
);
}
render() {
return (
<LinearGradient colors={[Colors.splashGradient.top, Colors.splashGradient.middle, Colors.splashGradient.bottom]}
style={{flex: 1}}>
<View style={styles.container}>
<IOSStatusBar backgroundColor="transparent"/>
{this.state.transactionDetails !== null ?
(this.state.transactionDetails.length > 0 &&
<Accordion sections={this.state.transactionDetails} renderHeader={this.renderHeader}
renderContent={this.renderContent} underlayColor={Colors.rowSeparatorBlue}
/>
) : <View/>
}
</View>
</LinearGradient>
);
}
}
如果我理解正确,那么SellAsset()是屏幕组件上的方法吗?
您有两种选择:
1.绑定这两种方法
class YourScreen extends React.Component {
constructor(props) {
this.renderContent = this.renderContent.bind(this);
this.sellAsset = this.sellAsset.bind(this);
}
sellAsset() { ... }
renderContent() { ... }
}
2.使用箭头功能
class YourScreen extends React.Component {
sellAsset = () => { ... }
renderContent = (item) => { ... }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.