I am trying to use react-native-picker
in my project and recently came across this Expo Snack: https://snack.expo.dev/HkM_BcGBW
I want to display content on the page depending on the current value of the picker. For instance, some text right below the picker saying "The option you have selected is [text of the currently-selected option in the picker]." How can I do this?
You can do as the example and use state value to display in the component
import React, { Component } from 'react';
import { Text, View, StyleSheet, Picker } from 'react-native';
export default class App extends Component {
constructor(props) {
super(props)
this.state = {
language: 'java',
}
}
render() {
return (
<View style={styles.container}>
<Text style={styles.title}>Unstyled:</Text>
<Picker
style={styles.picker} itemStyle={styles.pickerItem}
selectedValue={this.state.language}
onValueChange={(itemValue) => this.setState({language: itemValue})}
>
<Picker.Item label="Java" value="java" />
<Picker.Item label="JavaScript" value="js" />
<Picker.Item label="Python" value="python" />
<Picker.Item label="Haxe" value="haxe" />
</Picker>
<Text>The option you have selected is {this.state.language}</Text>
</View>
);
}
}
But do remember
onValueChange={(itemValue) => this.setState({language: itemValue})}
this stores value rather than the label.