问题
how can I pick 'label' of 'Picker.Item' from an array. i have my picker code as follows:
<View style={Styles.inputWrapper}>
<Picker
selectedValue={this.state.expiryYear}
style={Styles.expYear}
onValueChange={(itemValue, itemIndex) => this.setState({ expiryYear:
itemValue })}>
<Picker.Item label="select" value="0" />
<Picker.Item label="2018" value="2018" />
<Picker.Item label="2019" value="2019" />
<Picker.Item label="2020" value="2020" />
<Picker.Item label="2021" value="2021" />
.
.
.
<Picker.Item label="2030" value="2030" />
</Picker>
</View>
I have an array 'years[]' with values 2018,2019,...,2030
var years = [];
for (y = 2018; y <= 2030; y++) {
years.push([y]);
}
In dropdown list I want to show years
回答1:
You need to add below code in between your
<Picker> CODE </Picker>
years.map((year) => {
return (
<Picker.Item label={year} value={year} />
)
})
years is an array and has value whatever you want to render.(i mean how many years)
来源:https://stackoverflow.com/questions/53332389/how-to-display-picker-item-label-with-dynamic-values-in-array-in-react-native