import React from 'react';
export default class index extends React.Component {
constructor() {
super();
this.state = {
sayings: ""
};
this.update = this.update.bind(this);
}
update(e) {
this.setState({
sayings: e.target.value
});
}
render() {
return (
<div>
<input type="text" onChange={this.update} />
<br/>
{this.state.sayings}
</div>
);
}
}
import React from 'react';
export default class CustomTextInput extends React.Component {
constructor(props) {
super(props);
this.textInput = React.createRef();
this.getValueInput = this.getValueInput.bind(this);
}
getValueInput() {
console.log(this.textInput.current.value);
}
render() {
return (
<div>
<input type="text" ref={this.textInput} />
// Hoặc có thể sử dụng dài dòng <input type="text" ref={(node)=> this.textInput = node} />
<input type="button" value="Get Value Input" onClick={this.getValueInput} />
</div>
);
}
}