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
193 stars 90 forks source link

How can i set initial values selected? #84

Open Gugarauj07 opened 12 months ago

Gugarauj07 commented 12 months ago

In my profile page, i want to display in the dropdown the previously variables selected by the user.

LucasCoelho commented 11 months ago

you can use defaultOption

Gugarauj07 commented 10 months ago

you can use defaultOption

export default function App() {

  const [selected, setSelected] = useState([]);

  const data = [
    {key:'1',value:'Jammu & Kashmir'},
    {key:'2',value:'Gujrat'},
    {key:'3',value:'Maharashtra'},
    {key:'4',value:'Goa'},
  ]

  return(
    <MultipleSelectList 
        setSelected={(val) => setSelected(val)} 
        data={data} 
        save="value"
        onSelect={() => alert(selected)} 
        label="Categories"
        defaultOption={{ key:'1', value:'Jammu & Kashmir' }}   //default selected option
    />
  )
}

I tryed using it like this, but there is no default variable selected.

alveentino commented 10 months ago

you can use defaultOption

export default function App() {

  const [selected, setSelected] = useState([]);

  const data = [
    {key:'1',value:'Jammu & Kashmir'},
    {key:'2',value:'Gujrat'},
    {key:'3',value:'Maharashtra'},
    {key:'4',value:'Goa'},
  ]

  return(
    <MultipleSelectList 
        setSelected={(val) => setSelected(val)} 
        data={data} 
        save="value"
        onSelect={() => alert(selected)} 
        label="Categories"
        defaultOption={{ key:'1', value:'Jammu & Kashmir' }}   //default selected option
    />
  )
}

I tryed using it like this, but there is no default variable selected.

yeah, me too

xgenem commented 10 months ago

In my case adding defaultOption makes it unable to change value onSelect.

bastos12 commented 9 months ago

Same for me. When a checkbox is ticked, I assume it is stored in a state? The documentation details the properties of setSelected but not the state. Is there a possibility to have multiple default selected values? I can collaborate on that

natsu90 commented 7 months ago

i have same issue. the option is not selected.

edit: nevermind i got it working, defaultOption={{key: text, value: text}}