[英]React Form Input with initial text value
I am using React Bootstrap input components and would like an initial value for the input component, but I cannot see how to do this. 我正在使用React Bootstrap输入组件,并且想要输入组件的初始值,但是我看不到如何做到这一点。 Using
defaultValue
does not put a physical text into the input component, which is what I am trying to do. 使用
defaultValue
不会将物理文本放入输入组件,这是我正在尝试做的事情。 How do I do this? 我该怎么做呢? Thanks in advance.
提前致谢。
Here is my component: 这是我的组件:
<InputGroup>
<FormControl
aria-label="Default"
aria-describedby="inputGroup-sizing-default"
value = {this.state.name}
onChange={(event)=>{
this.setState({
name:event.target.value
});
}}
readOnly={ isReadOnly }
defaultValue={this.state.name} // ---> This is what should make the text read the pre-existing name
/>
</InputGroup>
It gets its value from state.name
so instead you should define default state on your component that holds the control. 它从
state.name
获取其值,因此您应该在保存控件的组件上定义默认状态。
For example in the constructor: 例如在构造函数中:
constructor(props: any) {
super(props)
this.state = {
name: 'default value',
}
}
If your form is to be pre-populated with existing server-side data, you might want to set it as the component loads (after a call to the backend). 如果要用现有的服务器端数据预填充表单,则可能需要在加载组件时(在调用后端之后)将其设置。 In which case you could hook into the
componentDidMount
method. 在这种情况下,您可以使用
componentDidMount
方法。 Read more about component life cycle methods here: https://reactjs.org/docs/react-component.html#the-component-lifecycle 在此处阅读有关组件生命周期方法的更多信息: https : //reactjs.org/docs/react-component.html#the-component-lifecycle
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.