Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to provide Picker a default "Please select..." option?

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>
like image 945
Luiz Avatar asked Feb 10 '17 21:02

Luiz


People also ask

How do I add a select option in react-native?

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.


1 Answers

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>
like image 59
Yo Wakita Avatar answered Oct 20 '22 15:10

Yo Wakita