0
const _DATA = [{
  code: 'code1',
  name: 'Jagger'
},{
  code: 'code2',
  name: 'Tigger'
},{
  code: 'code3',
  name: 'Lion'
}]

 <SelectPicker
      data={_DATA.map(x => {return {label: x.name, value: x.code} })}
      style={{ width: 224 }}
      defaultValue={_DATA[1].label}
    />

How do I set the value of code1 for example I have a form then it will set the code1 when opening the form/drawer. cause I have a list then I have edit button. when I try to select the data which is the code1 then it should automatically display. but on my side it didn't work.

1 Answers1

0

You don't have to map data. SelectPicker support pass valueKey and labelKey. And you can pass code value to SelectPicker.

const _DATA = [{
  code: 'code1',
  name: 'Jagger'
},{
  code: 'code2',
  name: 'Tigger'
},{
  code: 'code3',
  name: 'Lion'
}]

 <SelectPicker
      data={_DATA}
      valueKey="code"
      labelKey="name"
      style={{ width: 224 }}
      defaultValue={_DATA[0].code}
    />
superman66
  • 46
  • 4
  • I tried like this but it doesn't work also ```defaultValue={![null,undefined].includes(control.getValues()['code']) ? control.getValues()['building']['name'] : control.getValues()['code']}``` –  Mar 26 '21 at 03:16
  • yes, but when I try to console the _DATA[0] it work. but when I try to console with ['code'] its undefined –  Mar 26 '21 at 03:29
  • what is control.getValues()? – superman66 Mar 26 '21 at 03:54
  • ```const { control, register, errors } = useFormContext();``` –  Mar 26 '21 at 04:15
  • may be you can provider the complete code on codesanbox. – superman66 Mar 26 '21 at 07:55
  • According to the code you provided, it is difficult to find the reason. – superman66 Mar 26 '21 at 07:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/230431/discussion-between-superman66-and-beginner-coder). – superman66 Mar 27 '21 at 05:19