問題:
Warning: A component is changing an uncontrolled input of type text to be controlled.警告:組件正在更改要控制的文本類型的不受控制的輸入。 Input elements should not switch from uncontrolled to controlled (or vice versa).輸入元素不應從不受控制切換到受控制(反之亦然)。 Decide between using a controlled or uncontrolled input element for the lifetime of the component.*決定在組件的生命週期內使用受控或非受控輸入元素。*
Following is my code:以下是我的代碼:
constructor(props) {
super(props);
this.state = {
fields: {},
errors: {}
}
this.onSubmit = this.onSubmit.bind(this);
}
....
onChange(field, e){
let fields = this.state.fields;
fields[field] = e.target.value;
this.setState({fields});
}
....
render() {
return(
<div className="form-group">
<input
value={this.state.fields["name"]}
onChange={this.onChange.bind(this, "name")}
className="form-control"
type="text"
refs="name"
placeholder="Name *"
/>
<span style={{color: "red"}}>{this.state.errors["name"]}</span>
</div>
)
}
解決方案:
參考一: https://en.stackoom.com/question/3BG0n參考二: https://stackoom.com/question/3BG0n