question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onValueChange Event triggers every time render update

See original GitHub issue

Describe 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:open
  • Created 5 years ago
  • Reactions:33
  • Comments:62 (17 by maintainers)

github_iconTop GitHub Comments

12reactions
SmirnovM91commented, Jun 7, 2020

I’ve fixed it for me by adding key parametr to RNPickerSelect like this image

P.S. I use Mobx state management

10reactions
marcolangebeekecommented, Dec 30, 2020

I’ve fixed it for me by adding key parametr to RNPickerSelect like this image

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”

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native Picker: onValueChange triggers unintended ...
The state variable changes to the desired dropdown item for one render iteration and then automatically resets to the defaultPlaceholder value.
Read more >
The difference between onBlur vs onChange for React text ...
It doesn't matter if the value has changed or not, every time you get out of focus. The event will trigger. What is...
Read more >
SyntheticEvent - React
React normalizes events so that they have consistent properties across different browsers. The event handlers below are triggered by an event in the...
Read more >
Simulate React On-Change On Controlled Components
But triggering onChange without using setState is tricky. As for controlled components React swallows any event which sets only value .
Read more >
useForm | React Hook Form - Simple React forms validation
onChange, string, Validation will trigger on the change event with each input, and lead to multiple re-renders. Warning: this often comes with a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found