确定如何设置默认值

时间:2017-09-18 23:27:44

标签: reactjs redux react-redux

您好我是新来的反应和redux。我要做的是有一个编辑页面从我的服务器获取值并将其放入一个输入值,以便用户可以编辑该值。

<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>

反应组件内部。

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'
}

如何在输入中插入用户名。我尝试使用id和所有传统的javascript方法,但它不起作用。

1 个答案:

答案 0 :(得分:0)

看起来像是使用了反应引导程序 下面的示例使用defaultValue属性。 如果你想通过

处理输入值
<FormControl
            type="text"
            autoFocus
            onBlur={this.handleMessage}
            defaultValue={this.props.user.name}
/>           

...处理程序

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