react-select - 在反应选择中传递“事件”
问题描述
根据 react-select ( https://github.com/JedWatson/react-select ) 中的文档,有一个属性接受具有两个参数(值、事件)的函数。我需要得到这个事件。我在这里做错了吗?
解决方案
需要状态方程
class App extends Component {
state = {
firstValue: "",
secondValue: ""
};
handleChange = (value, state) => {
this.setState({ [state]: value });
};
render() {
return (
<Select
name="form-field-name"
value={this.state.value}
onChange={(value) => this.handleChange(value, "firstValue")}
options={[
{ value: "one", label: "One" },
{ value: "two", label: "Two" }
]}
/>
<Select
name="form-field-name"
value={this.state.value}
onChange={(value) => this.handleChange(value, "secondValue")}
options={[
{ value: "one", label: "One" },
{ value: "two", label: "Two" }
]}
/>
);
}
}
推荐阅读
- javascript - 如何让我的音频在 socket.io 中播放?
- java - 如何将我的方法从我的活动中放置或发送到另一个类来调用它?
- android - 如何从 Firestore 获取文档 ID 并在实时数据库路径中使用它?
- android - Android:单向绑定使 Switch OnCheckedChangeListener 不起作用
- gatsby - GatsbyJS 显示具有每个图像属性的图像目录
- hugo - Hugo - 如何在相关内容中显示图像
- react-native - React Native 项目不会建立在 expo 或 avd 上
- javascript - 当使用 csv 模块中的 csv-parser 将 csv 文件转换为 json 时,如何在 json 输出中获取标头作为密钥对?
- python - 查找多个值相同的元组的最大值
- recursion - 用于查找不动点的 OCaml 函数