javascript - 如何将选项中的额外键传递给 react-dropdown 并从 onChange 道具中取回它们?
问题描述
我正在使用这个下拉库react-dropdown
export const currencies = [
{ value: 'USD', label: '$ USD', currencySign: "$" },
{ value: 'INR', label: '₹ INR', currencySign: "₹" },
{ value: 'CAD', label: 'C$ CAD', currencySign: "C$" }
]
<Dropdown
options={currencies}
onChange={newSelectedOption => console.log(newSelectedOption)} // Getting an object with only 2 keys: "label" & "value". The third key "currencySign" is not there!
/>
有什么方法可以在 onChange 对象中获取“currencySign”?
谢谢!
2020 年 10 月 13 日更新:似乎 react-dropdown 库不提供此功能。
解决方案
如果您可以访问处理程序中的选项数组,则可以编写一个辅助函数
Dropdown
options={currencies}
onChange={onChange}
/>
onChange(selected){
var itemWithCurrency = this.findSelected(selected.value, this.currencies)
}
findSelected(value, items){
return items.find(item => item.value === value)
}
推荐阅读
- sql-server - 具有 ApplicationIntent 只读的 Azure SQL Server 弹性查询
- python - 为什么我的代码显示'for-loop'的效率高于'comprehension'的效率?
- scala - 值解析不是对象 org.joda.time.DateTime 的成员
- javascript - Javascript 将 ECDH 密钥转换为 ECDSA 密钥
- amazon-web-services - 如何启用来自 cloudFormation 文件的所有 RDS 流量?
- django - 使用 DRF create api 显示外键
- c# - 迁移是单独创建数据库,而不是表
- java - 字符串中带有反斜杠的正则表达式不起作用
- semantic-release - 我在 `master` 分支上的构建使用语义发布运行了两次
- react-native - 根据动态值选择 RadioButton