Skip to content

Warning: Function components cannot have string refs

Error message

                Function components cannot have string refs. We recommend using useRef() instead.
              

Quick summary

You used a string ref (e.g., `ref="myRef"`) on a function component, which is a legacy API not supported in function components.

Why this happens

  • Used ref="myInput" (string ref) instead of ref={myRef} (object ref)

  • Tried to pass a ref to a function component without forwardRef

Minimal example

✗ Broken code
<input ref="myInput" /> // String ref!
✓ Fixed code
const myInput = useRef(null);
// ...
<input ref={myInput} />

String refs are deprecated. useRef creates an object ref that works with function components and hooks.

How to diagnose

  • Look for string refs: ref="something"

  • Check if you're passing a ref to a function component

How to fix

  • Use useRef: const myRef = useRef() and ref={myRef}

  • Use React.forwardRef to pass refs to function components

How to prevent

  • Never use string refs — always use useRef

  • Use forwardRef when a function component needs to expose a ref

Related Resources

Related Glossary

Related Lessons

← Back to language