-
Notifications
You must be signed in to change notification settings - Fork 500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
onValueChange Event triggers every time render update #112
Comments
Please update your bug report with the missing information |
I updated my post |
Thanks. I’ll take a look. I assume you meant version 5.1.0? |
yes i have latest version |
you can solve by using if condition in on value change like this
|
Hi, When I change the value, the onValuechange fires, but its delayed and the value returned is always one behind. i.e the default value is 'Select a Category' the other values are Music, Poetry, Comedy.
Thank you for any help and thank you for RNPickerSelect |
@indrsidhu please create an example on snack.expo.io or share a github project with your issue -- I have tried to reproduce and I'm not having that issue (https://snack.expo.io/S1O1Q5914) @AndrewAi please create an example on snack.expo.io or share a github project with your issue --- nothing looks wrong in what you've shared (although I don't know what setEventCategory does. |
Yes i think you are setting state and then calling some |
Hi thank you for your comments. this is my setEventCategory function |
Try this way this will work |
Ok I will thank you, but I wont be able to try it till tomorrow. thanks |
I'm seeing the same issue but am unable to reproduce via snack.expo.io. I observed the following sequence:
I worked around this by removing the call nextProps.onValueChange(selectedItem.value, idx); from getDerivedStateFromProps. |
I experienced the exact same issue as @glenne. Removing |
I fixed mine by turning it into a controlled component like most of the other react-native components so rendering is only driven by props with not state memory. This actually simplifies a number of interactions. onValueChange(value, index) {
const { onValueChange } = this.props;
onValueChange(value, index);
}
static getDerivedStateFromProps(nextProps, prevState) {
const newItems = RNPickerSelect.handlePlaceholder({
placeholder: nextProps.placeholder
}).concat(nextProps.items);
const { selectedItem, idx } = RNPickerSelect.getSelectedItem({
items: newItems,
key: nextProps.itemKey,
value: nextProps.value
});
return {
items: newItems,
selectedItem: selectedItem
};
} |
@glenne @EyMaddis @jpandl19 would you mind checking if this branch solves the issue you were having? i'd ideally like to make as few breaking changes as possible, so this change will just prevent the internal selectedValue state change from occurring after an onValueChange call IF you have the |
@santanapaulo does this branch solve your issue? |
No @lfkwtz , It didn't work. I installed the version at that branch you said, but no happy ending :/ |
I was experiencing this in the iOS simulator. Disabling Hot Reloading from the Inspector seems to have fixed this in my case. |
I don't know why, but I fixed the extra render when I removed the props. |
Happy to look into this further if someone can provide a working demo that clearly identifies the issue |
It happened for me whenever I Example:
Whenever I change the value of the picker one time, the function |
@EyMaddis fork fixed this issue for me. Could this get merged? My problem was anytime the value was changed elsewhere, the callback was still called. Fixed for now. |
Thank @sarcadass - this worked for me! |
Hey everyone, same issue |
I'm having the same issue. (only on Android) But since the issue has been unresolved for over 1.5 years now, I might need to consider other options. @lfkwtz Is there any way I can help resolve the issue? My reproduction snack: |
feedback re: this PR would be appreciated |
This was driving me nuts and the above hack by @sarcadass worked for me in the meantime! |
@vreuling-bcs can you check #368? |
Ok, apologies for the delay but I tested #368 and unfortunately it did not solve this particular issue - the picker continued flickering back and forth between the old and new value, even with the above workaround in place. |
@jlampel can you provide a repro? |
would like to finally close this out - if someone could please provide a full repro of this issue on v8.0.4 or higher that would be appreciated |
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! ... Had the issue only on Android, using: |
I am experiencing this as well with latest. "expo": "^39.0.0"
import React, { FunctionComponent, useRef, useState } from 'react' const _style = StyleSheet.create({ interface State { export const TestComponent: FunctionComponent = (props) => {
} |
Using [email protected], [email protected]. Can confirm the following works without the issue:
As soon as I remove the key property, i get multiple console.logs (multiple re-renders) as I change values, so I can confirm the key property fix the issue. PS: Using functional components, and useState hook for pickerValue,setPickerValue |
You should use with useEffect. This's my solution: useEffect(() => { // call function }, [value]) |
This introduces another problem on iOS, the picker then closes automatically on setteling on a value, not allowing for continuous scrolling. |
Just adding my experience here if it helps someone: I'm trying to make the Picker only emit its value when Done is pressed. To do this I stored the index of the selected item and call The solution for me was to set the placeholder prop to an empty object. This disables the initial "label" that is shown at the top and prevents this infinite renders issue. Another solution for this would probably be to emit an onChange event for const selectedItem = items[index] as Item | undefined;
return (
<PickerSelect
placeholder={{}} // <- this fixed my issue
items={items}
value={selectedItem?.value ?? null}
onValueChange={(v, i) => setIndex(i)}
onDonePress={() => {
if (selectedItem) {
onValueChange(selectedItem.value, index);
}
}}
/>
); |
Thanks to @vikasdosi comment |
Seemingly because someone derailed this thread with a completely separate issue. I don't understand why such a critical issue hasn't been resolved after all this time either. |
I fixed this issue using the value prop
|
Hi, I solved this problem in another way.
|
version: "react-native-picker-select": "^8.0.4"; const [valuePicker, setValuePicker] = useState(''); |
You're a genius, thanks:) |
Checking to see if there are any fixes for this bug? |
If anyone is still having this issue, I'd like to add my two cents. I was having the same issue and somehow I solved it by changing the type of value in the items list. items={[ Hope it helps |
The same problem occurs when using "@react-native-picker/[email protected]" or later. This is related to the following issue, which is caused by the onValueChange param always being a string.
|
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):
Reproduction and/or code sample
Once select country i set in state using "handleChangeValue"
formData['country']
Whenever i change language it update my render()
I tried to debug
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
The text was updated successfully, but these errors were encountered: