Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

A ref on your own component never reaches the input

ErrorFunction components cannot be given refs. Attempts to access this ref will fail.

Short answer

A ref placed on your own component only works if that component passes it to a DOM element. React 18 needs forwardRef for that. React 19 accepts ref as a normal prop. With react-hook-form, a component that cannot take a ref must be wrapped in Controller instead of register.

  • Applies to React 18.3, React 19, react-hook-form
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find the component that receives the ref and look at whether it hands the ref to a real DOM element such as input or select.

    npm ls react
    grep -rn "register(" src | head
  2. 2Match what you see, then jump to its fix

    1. 1react 18.x installed; the spread {...register('name')} goes onto a custom componentReact 18 component without forwardRefFix
    2. 2react 19.x installed; the component takes props but never uses a refReact 19 component that does not pass ref onFix
    3. 3A UI-kit component with innerRef or inputRef, or one that only works through value and onChangeThe component calls its ref prop something else, or is controlledFix

What does the error look like?

  • Function components cannot be given refs. Attempts to access this ref will fail.
  • The field has no value and cannot be focused on errors

Cause 1 of 3

React 18 component without forwardRef

1Check

By default the DOM nodes of a component are private. A plain function component receives no ref, so the form library cannot reach the input.

npm ls react

2Fix

Wrap the component in forwardRef and attach the ref to the input.

import { forwardRef } from 'react';

const Select = forwardRef(function Select({ label, ...rest }, ref) {
  return (
    <label>
      {label}
      <select {...rest} ref={ref} />
    </label>
  );
});

3Verify

Submit the form and focus an invalid field. The browser focuses the select.

npm run dev

Confirmed by react.dev

Did this fix it?

Cause 2 of 3

React 19 component that does not pass ref on

1Check

From React 19, function components receive ref as an ordinary prop. If you destructure props and leave ref out, it never gets attached.

grep -rn "ref" src/components/Select.jsx

2Fix

Read ref from the props and put it on the input. forwardRef is no longer needed in new components.

function Select({ label, ref, ...rest }) {
  return (
    <label>
      {label}
      <select {...rest} ref={ref} />
    </label>
  );
}

3Verify

Trigger a validation error and confirm the field is focused.

npm run dev

Confirmed by react.dev

Did this fix it?

Cause 3 of 3

The component calls its ref prop something else, or is controlled

1Check

The react-hook-form docs say register reads the value through the ref of the real input. A spread onto a component that names its ref differently never assigns it.

grep -rn "innerRef\|inputRef" src node_modules/your-ui-kit/package.json 2>/dev/null

2Fix

For a differently named ref prop, pass the parts of register one by one. For a fully controlled component, use Controller.

const firstName = register('firstName', { required: true });

<TextInput
  name={firstName.name}
  onChange={firstName.onChange}
  onBlur={firstName.onBlur}
  inputRef={firstName.ref}
/>

3Verify

Type into the field and submit; the value appears in handleSubmit.

npm run dev

Confirmed by react-hook-form.com

Did this fix it?

How do I stop it coming back?

  • In a shared input component, always forward the ref (React 18) or the ref prop (React 19) to the real element.
  • Prefer Controller for third-party select and date components.
  • Keep one React major version across the app and its UI kit.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.