use ref in react class component code example
Example 1: use ref in component reactjs
function CustomTextInput(props) {
function handleClick() {
textInput.current.focus(); }
return (
<div>
<input
type="text"
ref={textInput} /> <input
type="button"
value="Donner le focus au champ texte"
onClick={handleClick}
/>
</div>
);
}
Example 2: use ref in component reactjs
class CustomTextInput extends React.Component {
}
Example 3: use ref in component reactjs
class CustomTextInput extends React.Component {
constructor(props) {
super(props);
this.textInput = React.createRef(); this.focusTextInput = this.focusTextInput.bind(this);
}
focusTextInput() {
this.textInput.current.focus(); }
render() {
return (
<div>
<input
type="text"
ref={this.textInput} /> <input
type="button"
value="Donner le focus au champ texte"
onClick={this.focusTextInput}
/>
</div>
);
}
}