The useRef is not a function error means React tried to call useRef as if it were a function, but it received something else entirely, usually because useRef wasn’t imported correctly from the react package.

Here are the most common reasons and how to fix them:

  1. Incorrect Import Statement: This is by far the most frequent culprit. You might have mistyped it, imported it from the wrong place, or it might be missing altogether.

    • Diagnosis: Examine your import statements at the top of the component file where you’re using useRef.
    • Fix: Ensure you have a line like this:
      import React, { useRef } from 'react';
      
      or if you’re using newer React versions with named exports exclusively:
      import { useRef } from 'react';
      
    • Why it works: This makes the useRef function available in your component’s scope, allowing React to find and execute it.
  2. Accidental Reassignment of useRef: Somewhere in your code, you might have accidentally overwritten the useRef variable with something else. This is less common but can happen in larger or more complex codebases.

    • Diagnosis: Search your component file for any assignments to a variable named useRef (e.g., useRef = someValue;).
    • Fix: Rename the variable you’re assigning to, or ensure you’re not accidentally using useRef as a variable name for something else. For example, if you have const useRef = 'myValue';, change it to const myRefVariable = 'myValue';.
    • Why it works: This prevents the original useRef function from being shadowed or replaced by a different value, ensuring React can access the correct function.
  3. Using useRef Outside a Functional Component or Custom Hook: Hooks like useRef can only be called at the top level of a React functional component or within another custom hook. They cannot be called inside class components, regular JavaScript functions, or event handlers.

    • Diagnosis: Check the file structure and where useRef is being called. Is it inside a class MyComponent extends React.Component { ... } block? Is it inside a nested function that isn’t a custom hook?
    • Fix: If you’re in a class component, you’ll need to use createRef() instead. If you’re in a regular function, move the useRef call to the top level of a functional component or a custom hook.
      • Class Component Equivalent:
        class MyComponent extends React.Component {
          myRef = React.createRef();
        
          render() {
            return <div ref={this.myRef}>Hello</div>;
          }
        }
        
    • Why it works: React’s hook rules mandate that hooks are only called in specific contexts. createRef() is the class component equivalent for managing mutable values that persist across renders.
  4. Typo in react Package Name: While less likely if other React features are working, a corrupted node_modules or a typo in the package name itself during installation or import could cause issues.

    • Diagnosis: Verify your package.json for the correct spelling of react. Also, check any module resolution configurations (like Webpack or Babel) if you’ve customized them extensively.
    • Fix: Ensure react is spelled correctly in package.json. If you suspect corruption, delete your node_modules folder and package-lock.json (or yarn.lock) and reinstall:
      rm -rf node_modules package-lock.json
      npm install
      # or
      rm -rf node_modules yarn.lock
      yarn install
      
    • Why it works: This ensures that the react package is correctly installed and accessible, and that useRef is being loaded from the actual React library.
  5. Outdated React Version: Very old versions of React might have different ways of handling hooks or might not support them at all.

    • Diagnosis: Check your package.json for the version of react. If it’s significantly old (e.g., pre-16.8, when Hooks were introduced), this could be the issue.
    • Fix: Update React to a version that supports Hooks (16.8 or later):
      npm install react@latest react-dom@latest
      # or
      yarn add react@latest react-dom@latest
      
    • Why it works: Newer versions of React have the useRef hook implemented according to the standard API.
  6. Incorrectly Mocking useRef in Tests: If you’re encountering this error specifically in a testing environment (e.g., Jest), it’s likely due to an incorrect mock setup.

    • Diagnosis: Review your Jest setup files or individual test files for any mocks related to react or react-dom.
    • Fix: Ensure your mocks correctly return a mock ref object. A common pattern is:
      // jest-setup.js or similar
      jest.mock('react', () => ({
        ...jest.requireActual('react'),
        useRef: jest.fn(() => ({ current: null })), // Mock useRef to return an object with a 'current' property
      }));
      
      Or for specific tests:
      import React from 'react';
      import { render, screen } from '@testing-library/react';
      
      // Mock useRef for this specific test file
      jest.mock('react', () => ({
        ...jest.requireActual('react'),
        useRef: jest.fn(),
      }));
      
      // ... your component usage ...
      
    • Why it works: Jest needs to know how to handle useRef when it encounters it during tests. By providing a mock implementation that mimics the expected behavior (returning an object with a current property), you satisfy React’s expectations.

After applying these fixes, the next error you might encounter, if your logic is still flawed, is a TypeError: Cannot read properties of null (reading 'focus') if you’re trying to use the ref on an element that hasn’t mounted yet or if the ref itself is null when you expect it to be an element.

Want structured learning?

Take the full React course →