为什么反应选择不更改标签,而是更改值?
const options = [
{ label: 'One', value: 'One' },
{ label: 'Two', value: 'Two' },
];
<Select
defaultValue={{label: "One", value: "One"}}
options=options
/>
答案 0 :(得分:3)
反应选择默认值使用一个对象来设置默认值。您代码中的问题非常简单,
1-您需要通过将选项包装在{}中来绑定选项,例如{options}
,然后设置defaultValue:
2- defaultValue={options[1]}
或defaultValue={{ label: 'Two', value: 'Two' }}
检查沙箱上的示例: sample example
为清楚起见,标签只会更改select中显示的文本,但实际值受对象中的value
键限制
答案 1 :(得分:0)
defaultValue属性将仅接受一个值。对于您的示例,如果将defaultValue设置为“ One”,它将自动将标签设置为上面的相应选项。一个例子是:
const options = [
{ label: 'One', value: 'One' },
{ label: 'Two', value: 'Two' },
];
//the defaultValue will look inside the options array and use the label where the value is the same
<Select
defaultValue="One"
options=options
/>