简体   繁体   English

反应如何设置默认值

[英]React how to set a default value

Hello I'm new to react and redux. 您好,我是新来的人,要做出反应并做些还原。 What I'm trying to do is have an edit page that gets the value from my server and puts it in an input value so the user can edit the value. 我要做的是有一个编辑页面,该页面从服务器获取值并将其放入输入值中,以便用户可以编辑该值。

<FormGroup>
    <Label htmlFor="name">Name *</Label>
    <div className="controls">
        <InputGroup>
           <Input id="name" size="16" type="text" value={this.props.user.name} />
        </InputGroup>
    </div>
</FormGroup>

Inside a react component. 在React组件内部。

constructor(props) {
    super(props)
    this.handleSubmit = this.handleSubmit.bind(this)
    this.componentWillMount = this.componentWillMount.bind(this)
}

componentWillMount(){
    // this will return my current user
    this.props.dispatch(fetchUser())
    console.log(this.props.user.name) //outputs 'John'
}

How can i insert the username inside the input. 我如何在输入中插入用户名。 I tried using the id and all the traditional javascript ways but its not working. 我尝试使用ID和所有传统的javascript方式,但无法正常工作。

It looks like used a react-bootstrap. 看起来像是使用了react-bootstrap。
Below sample use defaultValue property. 下面的示例使用defaultValue属性。 if you want to handle input value by 如果要通过以下方式处理输入值

<FormControl
            type="text"
            autoFocus
            onBlur={this.handleMessage}
            defaultValue={this.props.user.name}
/>           

handlers... 处理器...

handleMessage = (event) => {
  this.setState({
    userName: event.target.value,
  });
  // or do action
  // this.props.dispatch(notifyAction.changeUserName(event.target.value));
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM