fateh999 / react-native-paper-dropdown

Dropdown using react native paper TextInput and Menu
MIT License
130 stars 74 forks source link

Default value #51

Closed micutudor closed 1 month ago

micutudor commented 2 years ago

Hi, How I set a default value? Thanks!

haloriyan commented 2 years ago

I don't know if it is the best way or not, but I think I could share this because it is work for me

Open the file /node_modules/react-native-paper-dropdown/dist/DropDown.js and add this code right after displayValue state declaration

useEffect(() => {
    if (value != null) {
        list.map(l => {
            if (l.value == value) {
                setDisplayValue(l.label);
            }
        })
    }
}, [displayValue, setDisplayValue, value]);

Now when your value prop is same with any value of your list, displayValue will replaced with the matched value in your list.

I hope this could help you immediately, and sorry if this is not the best way to write due to I'm not expert in JS and React. And we all wish creator would add this feature soon

bombillazo commented 2 years ago

Made a PR for this: #75

Let's see if the maintainer can review and merge, this package has not been updated in over a year.