[英]Specify default value to FormControl of react-bootstrap
In react-bootstrap@0.24.5
I've used Input
attribute defaultValue
to specify start value selected in combobox在
react-bootstrap@0.24.5
我使用Input
属性defaultValue
来指定在组合框中选择的起始值
<Input type='select'
ref='templateSelect'
defaultValue={this.state.templateId}
onChange={this.handleTemplateChange}>
{options}
</Input>
How this should be handled in react-bootstrap@0.30.7
( newest one ) where Input
was deprecated and new component that should be used here FormControl
doesn't provide such attribute?这应该如何在
react-bootstrap@0.30.7
(最新的)中处理,其中Input
已弃用,并且应在此处使用的新组件FormControl
不提供此类属性?
Should value
be used instead?应该使用
value
吗?
<FormControl type='select'
ref='templateSelect'
value={this.state.templateId}
onChange={this.handleTemplateChange}>
{options}
</FormControl>
Or maybe something like this:或者可能是这样的:
value={this.state.templateId || 'default value'}
I didn't test this, but from the React Bootstrap source code for FormControl it seems like using defaultValue
prop should work:我没有对此进行测试,但是从FormControl的 React Bootstrap 源代码看来,使用
defaultValue
道具应该可以工作:
<FormControl type="select"
ref="templateSelect"
defaultValue={this.state.templateId}
onChange={this.handleTemplateChange}>
{options}
</FormControl>
If multi select defaultValue must be array:如果多选 defaultValue 必须是数组:
this.state = {
templateId:['some value']
}
<FormControl
multiple
type="select"
ref="templateSelect"
defaultValue={this.state.templateId}
onChange={this.handleTemplateChange}>
{options}
</FormControl>
With "react-bootstrap": "1.0.0-beta.14"
, the value
prop is used:使用
"react-bootstrap": "1.0.0-beta.14"
,使用value
prop:
<Form.Control as="select" value={user}>
{ users.map(opt => (<option>{ opt }</option>)) }
</Form.Control>
This way you can set the default value.这样您就可以设置默认值。
<option >is any default</option>
{
dataoption.map(item => {
return <option key={item.Value} vlaue={item.Value} selected={defaultselect ? defaultselect == item.Value ? true : false : false} >{item.Text}</option>
})
}
</FormControl>
You may receive an error in the console:您可能会在控制台中收到错误消息:
Warning: Use the defaultValue
or value
props on instead of setting selected
on .警告:使用
defaultValue
或value
道具 on 而不是设置selected
on 。
But setting defaultValue or value does not solve your problem但是设置 defaultValue 或 value 并不能解决您的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.