Search code examples
androidiosreactjsreact-nativehybrid-mobile-app

React Native Adding array of Picker


I wanted to add multiple pickers in React Native. I have a button when clicked a new picker should appear. I also want to store values of selected items of each picker in an array. I don't know how to make this possible. The callback of Picker onValueChange only sends item value and index in my case which can be same for 2 or more pickers. Please someone provide me a solution.


Solution

  • You can try to do something like this, in function handlePickerSelection you would be able to put value in array's element with index i

    let pickers = [];
    for ( let i = 0; i < numberOfPickers; i++) {
        pickers[i] = <Picker style={{height: 50, width: 100}} key={i} onValueChange={(value) => handlePickerSelection(value, i) }>
           <Picker.Item label="1" value={1}/>
           <Picker.Item label="2" value={2} />
       </Picker>
    }
    return (<View>{pickers}</View>)