lawnstarter / react-native-picker-select

🔽 A Picker component for React Native which emulates the native <select> interfaces for iOS and Android
https://npmjs.com/package/react-native-picker-select
MIT License
1.73k stars 490 forks source link

itemKey doesn't work #580

Open saaspeter opened 1 month ago

saaspeter commented 1 month ago

Describe the bug
according to the document: "Will attempt to locate a matching item from the items array by checking each item's key property". but in my testing in IOS, it don't work, but when I change itemKey to value field, it works. (the key and value are same value in my data)

To Reproduce
Steps to reproduce the behavior:

  1. set itemKey = 1 in code
  2. in UI, open the picker, then select the second item, press done.
  3. reopen the picker, found the selected item is still on the second item

Expected behavior
I expect the selected items in the above steps should be the first item(with key=1, also value=1). when I change the itemKey to value field, it works, everytime reopen the picker, the selected item always be the first.

according to the document, in IOS we should use itemKey, but it seems the value property works and itemKey don't work.

Screenshots
Add screenshots to help explain your problem. If screenshots aren't applicable to this issue, write "n/a".

Additional details

Reproduction and/or code sample
<RNPickerSelect onValueChange={(value) => setTimesADayValue(value)} itemKey={1} items={ BizType.getTimesADayArray() } ref={Platform.OS === 'ios' ? pickerRef2 : null} pickerProps={{ ref: Platform.OS === 'android' ? pickerRef2 : null }} style={pickerSelectStyles} useNativeAndroidPickerStyle={false} onDownArrow={()=>null} /> my data items like this: const timesADayArray = [{label: 'one time a day', value: 1, key: 1}]; for (let j = 2; j <= 12; j++) { timesADayArray.push({label: j+' times a day', value: j, key: j},); }

when I change itemKey field to 'value' field, it works.

athul-22 commented 4 weeks ago

https://github.com/lawnstarter/react-native-picker-select/pull/586

Please check this PR

saaspeter commented 3 weeks ago

@athul-22 , thank you, I will try this new updates. (currently I am using 'value', it works well :) )