如何在 React-Select 中通过ID设置选中值?
举个例子,我想根据所选的ID选中对应的标签值。
比如 id="2" 结果为 "Summer"。
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}
/>
);
}
}
在这里你可以查看我的CodeSandbox。 非常感谢您的帮助和指导。