首先,让我说,我搜索了很多关于这个问题,但我不能解决我的问题。
默认情况下,我有一个特定的值,我希望在视图中的选择框中显示该值。
选择代码:
<Select
options={getMainCategory.data.map((item) => {
return { value: item.id, label: item.categoryName };
})}
defaultValue={getMainCategory.data.find((x)=>{
const c = x.id === mainparentId;
return { value: c.id, label: c.categoryName };
})}
onChange={onOptionChange}
/>
我用上面的代码做了,但没有工作。如果你能指导我,我将不胜感激。
3条答案
按热度按时间whlutmcx1#
代码中的问题是你正在使用
defaultValue
函数,它必须是string
,number
,object
。在示例中,您必须使用value
属性,如下所示:kzmpq1sx2#
myss37ts3#
你可以这样使用它
在本例中,defaultValue属性被设置为与值“banana”匹配的选项对象。第一次渲染组件时,默认情况下将选择“香蕉”选项。
您可以将默认值更改为任何其他选项,方法是查找其对应的选项对象并将其作为defaultValue传递。