首页 > 解决方案 > 在反应选择中传递“事件”

问题描述

根据 react-select ( https://github.com/JedWatson/react-select ) 中的文档,有一个属性接受具有两个参数(值、事件)的函数。我需要得到这个事件。我在这里做错了吗?

代码:https ://codesandbox.io/s/62wrx9pm1k

标签: 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" }
        ]}
      />
    );
  }
}

推荐阅读