1

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 Answer 1

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)

Sign up to request clarification or add additional context in comments.

5 Comments

you need to define array first like:, var years = [2010, 2011, 2012, 2013, 2014];
var years = [2010, 2011, 2012, 2013, 2014]; tried with this again app stopped working. seems that something wrong with array data. @Hardik
now try with one single element like var years = [2000];
trying with var years = ["2018","2019","2020"]; will works.ie (data without quotes causes error) but not with for loop push
i doesnt need hardcorded data. need dynamic data. how it is possible?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.