onValueChange Event triggers every time render update
See original GitHub issueDescribe the bug Picker has function prop “onValueChange” as per expectation it should work like click event, when ever someone select value, but i found this event execute all the time when render() updates, so it creating problem, because it null my state value.
To Reproduce onValueChange = { (value) => console.log(“Event Triggered”+value)} Monitor console, it will execute all the time whenever render() update by any state or prop change.
Expected behavior It should execute only when we change dropdown value, after pick , not all the time
Smartphone (please complete the following information):
- Device: OnePlus2
- OS: Android
- react-native-picker-select version: 5.1.10
- react-native version: 0.55.4
- react version: 16.3.1 Reproduction and/or code sample
<PickerSelect
value={formData['country']}
placeholder={{label: I18n.t('select'), value: ''}}
style={pickerStyle}
onValueChange={(itemValue,itemIndex) => {
console.log("Country index="+itemIndex);
console.log("Country current="+formData['country']);
console.log("Country Changed="+itemValue);
this.handleChangeValue(itemValue,"country","picker")
}}
items={COUNTRY_RENDER}
/>
Once select country i set in state using “handleChangeValue” formData[‘country’] Whenever i change language it update my render() I tried to debug
console.log("Country index="+itemIndex);
console.log("Country current="+formData['country']);
console.log("Country Changed="+itemValue);
this.handleChangeValue(itemValue,"country","picker")
formData[‘country’] always return my selected country, but console.log(“Country Changed=”+itemValue);
does not return selected index once, i change language.
If language value is same for both english and spanish language, then it should return same index
Issue Analytics
- State:
- Created 5 years ago
- Reactions:33
- Comments:62 (17 by maintainers)
Top GitHub Comments
I’ve fixed it for me by adding key parametr to RNPickerSelect like this
P.S. I use Mobx state management
I tried all other solutions, but finally this did the job for me! Thanks @SmirnovM91
Set the key prop to the same state value as the value prop… how weird!
… key={this.state.country} value={this.state.country} …
Had the issue only on Android, using: “@react-native-picker/picker”: “^1.9.8” “react-native-navigation”: “^7.0.0”