react-native-dropdown-picker icon indicating copy to clipboard operation
react-native-dropdown-picker copied to clipboard

Cann't use dispatch function, can use only setState function

Open Einere opened this issue 3 years ago • 5 comments

setValue props only can assign setState function, and can't use dispatch function. so developers who want to use reducer must use setState, then process will be like below.

const [someList] = useState([ ... ]);
const [selected, setSelected] = useState(null);
const [state, dispatch] = useReducer(...);

return (
  <View>
    <DropDownPicker
        // ...
        value={selected} // i want to use reducer state, not local state.
        items={someList}
        setValue={setSelected}
        onChangeValue={(value) => dispatch(...)}
      />
  </View>
);

  1. user select item
  2. setSelected function trigger (it maybe cause re-rendering)
  3. onChangeValue function trigger
  4. dispatch (it maybe cause re-rendering also...)

i think it is unnecessarily long process and duplicated states. have you any plan to update it? if it support any function, would be more flexible and well-performance. 👏

Einere avatar Jun 01 '21 09:06 Einere

Hello,

I'll try to add support for dispatch functions.

hossein-zare avatar Jun 01 '21 17:06 hossein-zare

Hi again.

Adding support for dispatch functions failed, There's another way to accomplish that, I'll be back soon.

hossein-zare avatar Feb 04 '22 19:02 hossein-zare

any update on this?

alikarani avatar Jun 04 '22 17:06 alikarani

Hi, Any Hossein update? thank you for your efforts

atif9010 avatar Sep 05 '22 05:09 atif9010

I ran into this issue today while working with an old class component I wrote a few years back. This is an anti-pattern, but to make the lib work in the least complicated way, I used the static getDerivedStateFromProps lifecycle method. The older versions of React can use the componentWillReceiveProps method if needed.

This is what I did to make this work in a class component...

    static getDerivedStateFromProps(props, state) {
        if (props.val !== state.val) {
            return {
                value: props.val,
            };
        }

        return null;
    }

This is NOT a great solution but it does work in my testing so far... YMMV.

sentry0 avatar Feb 17 '23 19:02 sentry0