using useRef code example

Example 1: useRef

/*
	A common use case is to access a child imperatively: 
*/

function TextInputWithFocusButton() {
  const inputEl = useRef(null);
  const onButtonClick = () => {
    // `current` points to the mounted text input element
    inputEl.current.focus();
  };
  return (
    <>
      <input ref={inputEl} type="text" />
      <button onClick={onButtonClick}>Focus the input</button>
    </>
  );
}

Example 2: react useref in useeffect

import React, { useEffect, useRef } from 'react';

const fooComponent = props => {
	const inputBtnRef = useRef(null);
  	useEffect(() => {
      //Add the ref action here
      inputBtnRef.current.focus();
    });
  
  	return (
      <div>
        <input
          type="text"
          ref={inputBtnRef} 
		/>
      </div>
    );
}

Example 3: useRef() in react

function TextInputWithFocusButton() {
  const inputEl = useRef(null);
  const onButtonClick = () => {
    // `current` points to the mounted text input element
    inputEl.current.focus();
  };
  return (
    <>
      <input ref={inputEl} type="text" />
      <button onClick={onButtonClick}>Focus the input</button>
    </>
  );
}