通过
这些,都可通过 class NameForm extends React.Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
this.input = React.createRef();
}
handleSubmit(event) {
alert(‘A name was submitted: ‘ + this.input.current.value);
event.preventDefault();
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Name:
<input type="text" ref={this.input} />
</label>
<input type="submit" value="Submit" />
</form>
);
}
} 如上, 对应的 对于 class Form extends Component {
handleSubmitClick = () => {
const name = this._name.value;
// do something with `name`
}
render() {
return (
<div>
<input type="text" ref={input => this._name = input} />
<button onClick={this.handleSubmitClick}>Sign up</button>
</div>
);
}
} 可以看出, 对比之下, 对比与取舍虽然
所以如果需要上述这些东西,可以考虑 相关资源 |
React `controlled` 及 `uncontrolled` 组件
原文:https://www.cnblogs.com/Wayou/p/react_controlled_and_uncontrolled_component.html