我可以在 React Native 中的 ScrollView 属性周围添加自定义条件吗?



我想在我的 React Native 滚动视图属性中添加一个条件。这是我的代码:

<ScrollView ref={(ref) => this._refScrollView = ref}
onContentSizeChange={this.scrollToEnd} refreshControl={
if(pullToRefresh == 'conversations'){
<RefreshControl
refreshing={this.state.refreshing}
onRefresh={this._onRefresh.bind(this)}
/>
}
}>

虽然是给了我一个错误,但我只想问是否有任何可能的方法只有在PullToRefresh == 'conversations'时才RefreshControl部分。

你可以有条件地放refreshControl,试试以下

<ScrollView 
ref={(ref) => this._refScrollView = ref}
onContentSizeChange={this.scrollToEnd}
refreshControl={
(pullToRefresh == 'conversations') &&
<RefreshControl
refreshing={this.state.refreshing}
onRefresh={this._onRefresh.bind(this)}
/>
}
>
...
</ScrollView>

希望这会有所帮助!

我不知道你得到什么错误,但我认为这可能是语法错误。更改代码如下

<ScrollView 
ref={(ref) => this._refScrollView = ref}
onContentSizeChange={this.scrollToEnd} 
refreshControl={
(pullToRefresh == 'conversations') && {
<RefreshControl
refreshing={this.state.refreshing}
onRefresh={this._onRefresh.bind(this)}
/>
}
}
>

最新更新