← Learning Hub
Intermediate

useRef and Form Inputs

useRef gives you a box that keeps a value between renders without causing a re-render. Its two main jobs:

  • Pointing at a real DOM element, for example to focus an input.
  • Remembering something that isn't shown on screen, like a timer id.

Most data in React belongs in state, because changing state redraws the screen. But sometimes you need to remember something that the screen doesn't show - a timer id, the previous value of a prop - or you need to reach the real DOM element to focus it, scroll to it, or measure it. A ref is a small box for exactly these cases: it keeps its value across renders, and changing it doesn't cause a re-render.

Example
<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <div id="root"></div>

    <script src="https://unpkg.com/react@18.3.1/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@18.3.1/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/@babel/standalone@7.24.7/babel.min.js"></script>

    <script type="text/babel">
      const { useRef, useState } = React;

      function App() {
        const inputRef = useRef(null);
        const timer = useRef(null);
        const [seconds, setSeconds] = useState(0);

        function start() {
          if (timer.current) return;
          timer.current = setInterval(() => setSeconds(s => s + 1), 1000);
        }
        function stop() {
          clearInterval(timer.current);
          timer.current = null;
        }

        return (
          <div>
            <input ref={inputRef} placeholder="Search..." />
            <button onClick={() => inputRef.current.focus()}>Focus the box</button>
            <h3>⏱ {seconds}s</h3>
            <button onClick={start}>Start</button>
            <button onClick={stop}>Stop</button>
          </div>
        );
      }

      ReactDOM.createRoot(document.getElementById("root")).render(<App />);
    </script>
  </body>
</html>
▶ Try it yourself

Controlled vs uncontrolled inputs

  • Controlled: React state holds the value (value={name} onChange={...}). Best when you check or change the text as the user types.
  • Uncontrolled: the browser holds the value, and you read it when needed with a ref or from the form data. Simple for big forms.
Example
<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <div id="root"></div>

    <script src="https://unpkg.com/react@18.3.1/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@18.3.1/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/@babel/standalone@7.24.7/babel.min.js"></script>

    <script type="text/babel">
      const { useState } = React;

      function App() {
        const [pin, setPin] = useState("");
        const valid = /^[1-9][0-9]{5}$/.test(pin);

        function submit(e) {
          e.preventDefault();
          const data = new FormData(e.target);     // uncontrolled: read all boxes at once
          alert("City: " + data.get("city") + ", PIN: " + pin);
        }

        return (
          <form onSubmit={submit}>
            <input name="city" placeholder="City" />
            <input value={pin} onChange={e => setPin(e.target.value.replace(/\D/g, ""))}
                   placeholder="PIN code" maxLength={6} />
            <span>{pin && (valid ? " ✅" : " ❌")}</span>
            <button disabled={!valid}>Save</button>
          </form>
        );
      }

      ReactDOM.createRoot(document.getElementById("root")).render(<App />);
    </script>
  </body>
</html>
▶ Try it yourself
Changing ref.current does not update the screen. If the user should see a value, keep it in state instead.
Key points
  • useRef keeps a value between renders without re-rendering.
  • Use refs for DOM access (focus, scroll) and non-visual values.
  • Controlled inputs use state; uncontrolled inputs use refs.

📝 Test yourself

1. What happens when you change ref.current?
2. Which is a controlled input?
3. Fill in the hook that points at a DOM element:
const box = (null);