reactjs - react-select 不会显示默认值
问题描述
我正在尝试根据一组值在 react-select 中显示默认值。我已确认该值等于我要比较的值。我浏览了这篇文章,这篇文章,这篇文章,这篇文章,这篇文章和这篇文章。尽管有相似之处,但defaultValue
仍然没有设定。我已经通过登录确认我的比较器的结果是相同的,都是大写的,都是字符串。
我的选择元素是
<Select
name="disposition"
classNamePrefix="select"
options={statusOptions}
ref={selectInputRefPlan}
styles={singleStylesRowComp}
defaultValue={statusOptions.filter(
({ value }) => value.value === lead.disposition
)}
/>
我的数组是
const statusOptions = [
{ value: "HOT", label: i18n._(t`HOT`) },
{ value: "WIP", label: i18n._(t`WIP`) },
{ value: "LOST", label: i18n._(t`LOST`) },
{ value: "DNC", label: i18n._(t`DNC`) },
{ value: "WON", label: i18n._(t`WON`) },
{ value: "NEW", label: i18n._(t`NEW`) },
];
我正在测试value
而不是label
,因此不会出现翻译问题。当我记录响应时value.value === 'NEW'
,lead.disposition === 'NEW'
但是,defaultValue
不会设置。我也尝试过 prop value
,并阅读了 6 或 7 个类似的帖子。有简单的语法错误吗?还是我缺少的其他东西?
解决方案
错误在您的过滤器功能中。您分解 value,然后访问 value.value。这是未定义的。
相反,采用不分解的选项参数,并访问 option.value。
<Select
name="disposition"
classNamePrefix="select"
options={statusOptions}
ref={selectInputRefPlan}
styles={singleStylesRowComp}
defaultValue={statusOptions.filter((option) => option.value === lead.disposition)}
/>;
推荐阅读
- ruby-on-rails - Rails 6 测试的问题
- c++ - 错误 C2677:二进制“*”:未找到全局运算符
- php - 使 AJAX 分页在刷新时与 (AJAX) URL 过滤器参数一起工作
- laravel - 尝试启用 php_fileinfo 扩展
- ruby - Savon 中的多部分 SOAP 请求
- python - Rediness & Livenesss 以随机间隔失败
- java - 如何在连接到 Mongo 存储库的 Java/Spring 控制器上引发异常?
- r - 使用 1 个循环来自不同数据帧的子集
- python - 在 Excel 中创建表 - Python openpyxl
- android - Android Firestore“whereEqualTo”和“whereArrayContains”以及orderBy子句不起作用