反应选择将选项字段的名称从({值:``,标签:''})更改为({id:``,名称:''})

use*_*421 1 reactjs react-select

有没有办法更改选项对象的字段?

从我的BE API中,我得到:

const items = [{ id: 1, name:'dropdown1'}, { id: 2, name:'dropdown2'}];

因此,现在我必须将字段重新映射到valuelabel,如果可以动态设置这些字段,那就太好了,也许是这样的:

    <Select
      optionRemapping={{value: 'id', label: 'name'}}
      options={items}
    />
Run Code Online (Sandbox Code Playgroud)

或者,也许我错过了人们如何做到这一点的模式?

进行以下操作感觉有点不对。

items.map((item) => {
  return { value: item.id, label: item.name };
});
Run Code Online (Sandbox Code Playgroud)

Moh*_*ami 5

使用getOptionLabelgetOptionValue道具。

<Select
  getOptionLabel={option => option.name}
  getOptionValue={option => option.id}
  options={items}
/>
Run Code Online (Sandbox Code Playgroud)

示例:https//codesandbox.io/s/kmoyw8q667