本文介绍了如何为 Picker 提供默认的“请选择...";选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想让我的 Picker 在启动时显示默认选项".这意味着:类似于请选择一个选项".
I'd like to make my Picker to show a "default option" on startup. That means: something like "Please select an option".
我尝试用这个短语手动添加一个选项,但是,这样可以在选择其他选项后重新选择默认选项",就像它是真正的选项之一.有没有办法做到这一点?
I've tried to add a option manually with this phrase, but, this way the "default option" can be re-selected after selecting other options, like it was one of the real options. There is some way to do this?
<View style={Styles.row}>
<Picker
selectedValue={this.state.initialOption}
onValueChange={this.handleChangeOption}
prompt='Options'}
>
<Picker.Item label='Please select an option...' value='0' />
<Picker.Item label='option 1' value='1' />
<Picker.Item label='option 2' value='2' />
</Picker>
</View>
推荐答案
您可以向 handleChangeOption 添加条件,以便它仅在值不为 0 时设置状态(请选择一个选项...
选择器).类似的东西:
You can add a conditional to your handleChangeOption so that it would only set state when the value is not 0 (the Please select an option...
Picker). Something like:
handleChangeOption(val) {
if (val !== 0) {
this.setState({selectedValue: val});
}
}
...
<View style={Styles.row}>
<Picker
selectedValue={this.state.selectedValue}
onValueChange={this.handleChangeOption}
prompt='Options'}
>
<Picker.Item label='Please select an option...' value='0' />
<Picker.Item label='option 1' value='1' />
<Picker.Item label='option 2' value='2' />
</Picker>
</View>
这篇关于如何为 Picker 提供默认的“请选择...";选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!