How to display Picker.Item label with Dynamic values in array in react native

南楼画角 提交于 2020-03-05 03:39:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!