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.74k stars 499 forks source link

onValueChange is being called twice after new value is selected #421

Closed otaviobonder-deel closed 3 years ago

otaviobonder-deel commented 3 years ago

Describe the bug

When trying to dispatch an onValueChange to update the Redux state, the function is called three times and the value is reset to the initial value. It may be related to https://github.com/lawnstarter/react-native-picker-select/issues/369

The related issue was closed, but I think the problem persist, since there are more people in that issue saying the same.

To Reproduce
Steps to reproduce the behavior:

  1. Open the picker
  2. Select another value (onValueChange is called once and the value seems to update correctly)
  3. Click done (onValueChange is called twice with the initial value)
  4. Value returns to the initial value

Expected behavior

The onValueChange should be called once and the new value must persist.

Screenshots

There are two situations. The first one is when you change only once the value:

https://user-images.githubusercontent.com/28715087/110009846-56839980-7cfc-11eb-8ade-15a38d35ee83.mov

The other is when you change more than once the value:

https://user-images.githubusercontent.com/28715087/110009945-7b780c80-7cfc-11eb-96dd-8d094d4aa926.mov

Additional details

Reproduction and/or code sample

My onValueChange is dispatching an action to the reducer, just changing the state:

<Picker
  items={pollIntervalOptions}
  value={pollInterval}
  onValueChange={(value) => {
    dispatch(updateChartOptions({pollInterval: value as PollInterval}));
  }}
/>

The pollInterval variable is coming from redux: const {pollInterval} = useTypedSelector((state) => state.chart);

otaviobonder-deel commented 3 years ago

I'm gonna close this issue because it was my mistake. I was wrongfully deconstructing an object. Sorry