The useReducer is not a function error typically means React’s internal state management mechanism is being called in a way it doesn’t expect, often due to a mismatch between how it’s imported and how it’s being used, or a corrupted dependency.

Here are the common culprits and how to fix them:

  • Incorrect Import: You might be importing useReducer from the wrong module or misspelling it. This is the most frequent cause.

    • Diagnosis: Check your import statements at the top of the file where you’re using useReducer.
    • Fix: Ensure it’s imported directly from react:
      import React, { useReducer } from 'react';
      
    • Why it works: This correctly brings the useReducer hook into your component’s scope from React’s core library.
  • Typo in Usage: Even if imported correctly, a simple typo when calling the hook can lead to this error.

    • Diagnosis: Search your component code for useReducer and verify the spelling.
    • Fix: Correct any spelling mistakes, ensuring it’s exactly useReducer:
      const [state, dispatch] = useReducer(reducer, initialState);
      
    • Why it works: This matches the function name exactly as exported by React, allowing JavaScript to find and execute it.
  • Older React Version or Incorrect Installation: If you’re on a very old version of React or if your node_modules directory is in an inconsistent state, useReducer might not be available or correctly linked.

    • Diagnosis: Check your package.json for your React version. It should be ^16.8.0 or higher for hooks to be supported. Then, delete your node_modules folder and package-lock.json (or yarn.lock) and reinstall.
    • Fix:
      1. Delete node_modules directory.
      2. Delete package-lock.json (or yarn.lock).
      3. Run npm install (or yarn install).
    • Why it works: This ensures you have a compatible React version and that all dependencies are installed cleanly and consistently.
  • Transpilation Issues: If you’re using a custom Babel configuration or a bundler setup, it might be misinterpreting or failing to transpile hooks correctly, especially if you’re not targeting modern JavaScript environments.

    • Diagnosis: Review your Babel configuration (.babelrc or babel.config.js) and your bundler’s configuration (Webpack, Parcel, etc.) to ensure they are set up to handle React hooks. Specifically, check for plugins like @babel/plugin-transform-react-jsx and @babel/plugin-transform-react-jsx-self or similar in your presets.
    • Fix: Update your Babel configuration to include the necessary presets and plugins for React hooks. A common setup for modern React might look like this in .babelrc:
      {
        "presets": [
          "next/babel", // If using Next.js
          ["@babel/preset-env", { "modules": false }],
          ["@babel/preset-react", { "runtime": "automatic" }]
        ]
      }
      
      If not using Next.js, ensure @babel/preset-react is configured correctly, especially the runtime option.
    • Why it works: Proper transpilation ensures that the JSX and hook calls are converted into standard JavaScript that browsers can understand, including the correct handling of React.useReducer.
  • Circular Dependencies: In complex applications, a circular dependency between modules can sometimes lead to a situation where a module is imported before it’s fully initialized, causing functions like useReducer to appear undefined or not a function.

    • Diagnosis: Use tools like madge or your bundler’s analysis features to detect circular dependencies in your project.
    • Fix: Refactor your code to break the circular dependency. This might involve moving shared logic to a new module, changing import/export patterns, or using dynamic imports.
    • Why it works: By resolving circular dependencies, you guarantee that react and its exported functions are fully loaded and available before they are called.
  • Incorrect react and react-dom Version Mismatch: If you have different versions of react and react-dom installed, or if one is installed and the other is missing, it can cause unexpected behavior with hooks.

    • Diagnosis: Check your package.json and run npm ls react react-dom (or yarn list react react-dom) to see the installed versions. They should be identical and compatible.
    • Fix: Ensure both react and react-dom are listed with the same version in your package.json and reinstall. For example, in package.json:
      "dependencies": {
        "react": "^18.2.0",
        "react-dom": "^18.2.0"
      }
      
      Then run npm install (or yarn install).
    • Why it works: React hooks rely on the internal consistency between the react and react-dom libraries. A mismatch can break this internal contract.

The next error you might encounter after fixing this is a TypeError: dispatch is not a function if your reducer logic itself is flawed or if the dispatch function from useReducer is being called incorrectly.

Want structured learning?

Take the full React course →