reactjs - React-select onchange 事件多重属性
问题描述
这是我的问题/问题。使用当前版本的#react-select,on-change,事件包含值和标签。但是如何设置它以返回更多道具?例如,我正在选择一个邮政编码,但我希望它返回城市和该城市的呼叫代码。我在#react-select 的选项数组中确实有该信息
谢谢你。
解决方案
感谢@steve-cutter-blades 的评论,这是正确的,所以我会更新我的答案:
2019 年 12 月 20 日更新
onChange 函数从您作为道具传递的选项中返回完整的 optionObject。因此将返回每个选项中的所有键:
import React, { Component } from "react";
import ReactDOM from "react-dom";
import Select from "react-select";
class App extends Component {
onChange = (option) => {
// option.value --> "chocolate"
// option.label --> "Chocolate"
// option.extraInfo --> "A"
return option;
};
render() {
const options = [
{ value: "chocolate", label: "Chocolate", extraInfo: "A" },
{ value: "strawberry", label: "Strawberry", extraInfo: "B" },
{ value: "vanilla", label: "Vanilla", extraInfo: "C" }
];
return (
<div>
<Select
options={options}
onChange={this.onChange}
isMulti={false}
closeMenuOnSelect={true}
/>
</div>
);
}
}
export default App;
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
我之前的答案有效,但解决方法很糟糕。
推荐阅读
- ruby - 混合内容错误,重启puma解决
- mybatis - MyBatis 映射集合内部关联
- python - 我无法通过代理配置使用 PySpark
- routes - 路由、AuthGuard 和加载子模块
- c# - 在 URL 重写/ARR 之前捕获 IIS 上的 http 流量
- javascript - 如何使用 google outh 对我的数据库(firebase 或 firestore)中的那些用户进行身份验证?
- python - Stripe -> 通过代码配置客户门户时的问题
- python - 如何获得完成 Celery 任务所需的估计时间?
- c# - 你可以使用 lambda 表达式和 .find 吗?
- javascript - Discord.js - awaitReactions 在捕获超时错误时给出错误