React useRef Hook

PHOTO EMBED

Wed Sep 21 2022 03:39:19 GMT+0000 (Coordinated Universal Time)

Saved by @joseleal #jsx

import { useRef } from "react";
import ReactDOM from "react-dom/client";

function App() {
  const inputElement = useRef();

  const focusInput = () => {
    inputElement.current.focus();
  };

  return (
    <>
      <input type="text" ref={inputElement} />
      <button onClick={focusInput}>Focus Input</button>
    </>
  );
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);
content_copyCOPY

https://www.w3schools.com/react/react_useref.asp