作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我用过 react-select在我的显示选择框的应用程序中。下面是我选择的代码。
<Select
className="custom-form-control mb-2"
name="organization_options"
options={this.organizationOptions()}
placeholder={false}
onChange={this.handleChange.bind(this)}
value={selectedValue === "" ? this.organizationOptions()[0] : selectedValue}
/>
0: {value: "62", label: "Dfdf"}
1: {value: "128", label: "Dfdfdsf"}
2: {value: "151", label: "Fgfdgdfh"}
3: {value: "121", label: "Hhhfas"}
4: {value: "55", label: "My Sensor_56"}
5: {value: "13", label: "New Org"}
6: {value: "44", label: "Org 2"}
7: {value: "148", label: "Testing App"}
selectedValue='55'
defaultValue={this.organizationOptions().find(op => {
return op.value === selectedValue
})}
defaultValue={{value: "55", label: "My Sensor_56"}}
最佳答案
这里我们在组织选项中找到默认值并将其作为值传递给 Select:
import React from 'react';
import Select from 'react-select';
export default class App extends React.Component {
organizationOptions = () => {
return [
{value: "62", label: "Dfdf"},
{value: "128", label: "Dfdfdsf"},
{value: "151", label: "Fgfdgdfh"},
{value: "121", label: "Hhhfas"},
{value: "55", label: "My Sensor_56"},
{value: "13", label: "New Org"},
{value: "44", label: "Org 2"},
{value: "148", label: "Testing App"}
]
}
render() {
const selectedValue = "55"
return (
<Select
value={this.organizationOptions().find(op => {
return op.value === selectedValue
})}
onChange={this.handleChange}
options={this.organizationOptions()}
/>
);
}
}
关于reactjs - React - 如何在 react-select 中设置默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52436805/
我是一名优秀的程序员,十分优秀!