如何在 React JS 中为下拉菜单设置占位符?
How to set placeholder for dropdown in react js?
<select>
<option selected disabled>Select</option
我添加了一个禁用 属性 的额外选项,但这给了我以下警告:
Use the defaultValue
or value
props on select instead of setting
selected
on option.
<select>
{this.props.optionarray.map(function(e){
return <option
value={e[self.props.unique]}
key={e[self.props.unique]}
>
{e[self.props.name]}
</option>
})}
</select>
optionarray
是一个 array
,它通过 props
传递到每个具有 object
的索引上,我通过 [=17= 传递密钥] 以及它在数组中。一切都在这里工作,它只是向我显示我上面提到的警告。
如何删除它或如何为 React 中的下拉菜单设置完美的占位符?
原因是,React
通过使用 states
提供了一种更好的控制元素的方法,因此不要将 selected
与选项一起使用,而是使用 属性 的值 select
并在 state
变量中定义其值,使用 onChange
方法更新 state
,这样使用:
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
value: '1'
};
}
render(){
return(
<select value={this.state.value} onChange={(e)=>{this.setState({value: e.target.value})}}>
<option value='1' disabled>Select</option>
{
[2,3,4].map((i,j)=>{
return <option key={i} value={i}>{i}</option>
})
}
</select>
);
}
}
How to set placeholder for dropdown?
根据 Mozilla Dev Network,placeholder 不是 <select>
上的有效属性。因此,您也可以代替它来呈现 default
选项:
<option default>Select</option>
根据DOC使用密钥:
Keys help React identify which items have changed, are added, or are
removed. Keys should be given to the elements inside the array to give
the elements a stable identity.
建议:
每当我们在循环中创建任何ui
dynamically
时,我们需要为每个element
分配一个unique
键,这样react
就可以很容易地识别元素,它基本上用来改进performance
.
检查工作示例:
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
value: '1'
};
}
render(){
return(
<select value={this.state.value} onChange={(e)=>{this.setState({value: e.target.value})}}>
<option value='1' disabled>Select</option>
{
[2,3,4].map((i,j)=>{
return <option key={i} value={i}>{i}</option>
})
}
</select>
);
}
}
ReactDOM.render(<App />, document.getElementById('container'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='container'/>
检查 fiddle 工作示例:https://jsfiddle.net/29uk8fn0/
<select>
<option selected disabled>Select</option
我添加了一个禁用 属性 的额外选项,但这给了我以下警告:
Use the
defaultValue
orvalue
props on select instead of settingselected
on option.
<select>
{this.props.optionarray.map(function(e){
return <option
value={e[self.props.unique]}
key={e[self.props.unique]}
>
{e[self.props.name]}
</option>
})}
</select>
optionarray
是一个 array
,它通过 props
传递到每个具有 object
的索引上,我通过 [=17= 传递密钥] 以及它在数组中。一切都在这里工作,它只是向我显示我上面提到的警告。
如何删除它或如何为 React 中的下拉菜单设置完美的占位符?
原因是,React
通过使用 states
提供了一种更好的控制元素的方法,因此不要将 selected
与选项一起使用,而是使用 属性 的值 select
并在 state
变量中定义其值,使用 onChange
方法更新 state
,这样使用:
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
value: '1'
};
}
render(){
return(
<select value={this.state.value} onChange={(e)=>{this.setState({value: e.target.value})}}>
<option value='1' disabled>Select</option>
{
[2,3,4].map((i,j)=>{
return <option key={i} value={i}>{i}</option>
})
}
</select>
);
}
}
How to set placeholder for dropdown?
根据 Mozilla Dev Network,placeholder 不是 <select>
上的有效属性。因此,您也可以代替它来呈现 default
选项:
<option default>Select</option>
根据DOC使用密钥:
Keys help React identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array to give the elements a stable identity.
建议:
每当我们在循环中创建任何ui
dynamically
时,我们需要为每个element
分配一个unique
键,这样react
就可以很容易地识别元素,它基本上用来改进performance
.
检查工作示例:
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
value: '1'
};
}
render(){
return(
<select value={this.state.value} onChange={(e)=>{this.setState({value: e.target.value})}}>
<option value='1' disabled>Select</option>
{
[2,3,4].map((i,j)=>{
return <option key={i} value={i}>{i}</option>
})
}
</select>
);
}
}
ReactDOM.render(<App />, document.getElementById('container'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='container'/>
检查 fiddle 工作示例:https://jsfiddle.net/29uk8fn0/