小编yas*_*ati的帖子

如何在react-select中设置值

如何在react-select中通过id设置值 例如,我想根据id的值来选择标签值。id =“2”结果=“夏天”

class MyComponent extends React.Component {
  constructor(props) {
    super(props);

    this.state = { selectedOption: "" };
  }

  options = [
    { id: "1", value: "Spring", label: "Spring" },
    { id: "2", value: "Summer", label: "Summer" },
    { id: "3", value: "Autumn", label: "Autumn" },
    { id: "4", value: "Winter", label: "Winter" }
  ];

  handleChange = selectedOption => {
    this.setState({ selectedOption });
  };

  render() {
    return (
      <Select
        value={this.state.selectedOption}
        onChange={this.handleChange}
        options={this.options}
      />
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

在这里您可以看到我的CodeSandbox。预先感谢您的帮助和指导

reactjs react-select

6
推荐指数
2
解决办法
2万
查看次数

标签 统计

react-select ×1

reactjs ×1