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>
To work with react-native-picker-select , we must import the RNPickerSelect component: import RNPickerSelect from "react-native-picker-select"; This component is then reused in our code to render the select view.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With