react refs in functional component code example
Example 1: cre&atRefs react js
const node = this.myRef.current;
Example 2: createref in functional component
function CustomTextInput(props) {
// textInput must be declared here so the ref can refer to it
const textInput = useRef(null);
function handleClick() {
textInput.current.focus();
}
return (
);
}
Example 3: use ref in component reactjs
function MyFunctionComponent() { return ;
}
class Parent extends React.Component {
constructor(props) {
super(props);
this.textInput = React.createRef(); }
render() {
// Ça ne fonctionnera pas !
return (
);
}
}
Example 4: use ref in component reactjs
class CustomTextInput extends React.Component { // ...
}