danish1658 / react-native-dropdown-select-list

☝️ React Native Select List Equivalent to Html's Select with options"
https://www.npmjs.com/package/react-native-dropdown-select-list
MIT License
201 stars 90 forks source link

Add default option prop #6

Closed westerandr closed 2 years ago

westerandr commented 2 years ago

Set default option of select list if defaultOption prop specified.

westerandr commented 2 years ago

This resolves #5.

danish1658 commented 2 years ago

Thanks for your interest in this package,

Can you tell me why we should not set our default value like this 👇

const [selected, setSelected] = React.useState(1); //Where 1 is the default selected id

This also nullifies your Issue raised #5 , as state variable persists for each render.

Still, if there is something i dont understand , please elborate a bit (why we should use default value prop)

westerandr commented 2 years ago

Hi sir,

So for example, in this component say I provide a list of objects.

[ { key: 1, value: 'Apples' }, { key: 2, value: 'Bananas' } ]

I am keeping track of global state, so if user pick 'Bananas', the state variable will stay selected as 'Bananas'.

But if I want to exit out of the page, and come back again. The state variable will still be 'Bananas' but the component won't show/render 'Bananas', instead it will show/render 'Select an Option' or whatever you suggested which was default selected id 1.

Hope that makes sense.

danish1658 commented 2 years ago

Yes it does make sense to me now, Thanks for elaborating,

I will approve this PR

westerandr commented 2 years ago

Thank you very much sir for this awesome package.