react-native-dropdown-picker
react-native-dropdown-picker copied to clipboard
Cann't use dispatch function, can use only setState function
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>
);
- user select item
-
setSelected
function trigger (it maybe cause re-rendering) - onChangeValue function trigger
- 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. 👏
Hello,
I'll try to add support for dispatch functions.
Hi again.
Adding support for dispatch functions failed, There's another way to accomplish that, I'll be back soon.
any update on this?
Hi, Any Hossein update? thank you for your efforts
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.