2 回答

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超3個(gè)贊
根據(jù)文檔,該Dropdown
組件采用value
道具。
import Dropdown from 'react-dropdown'
import 'react-dropdown/style.css'
class App extends React.Component {
constructor() {
super();
this.state={
cities: ["New York", "San Francisco", "Seattle", "Washington DC"],
selectedCity: ""
}
}
handleSelectCity = (option)=> {
const selectedCity = option.value
this.setState({selectedCity});
}
render() {
const {selectedCity, cities} = this.state;
return (
<Dropdown
options={cities}
onChange={this.handleSelectCity}
value={selectedCity}
placeholder="Select an option"
/>
)
}
}
export default App;

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊
在Dropdown組件中,您可以執(zhí)行以下操作
{(this.props.options.length > 1) ? <option>{this.props.placeholder}</option>: null}
瀏覽器將自動(dòng)選擇列表中的第一個(gè)選項(xiàng)。
添加回答
舉報(bào)