The useState is not a function error means that your React code is trying to call useState as if it were a function, but it’s not available in the current scope, or it’s been redefined.
This usually happens because React itself hasn’t been imported correctly, or because a file with the same name as a React internal module is shadowing the actual import.
Here are the most common reasons and how to fix them:
-
Incorrect Import of React: You might have forgotten to import
ReactoruseStateat all, or you’ve imported it incorrectly.- Diagnosis: Look at the top of your component file. Do you see
import React, { useState } from 'react';orimport { useState } from 'react';? - Fix: Ensure your imports are correct. For functional components,
import { useState } from 'react';is usually sufficient. If you’re using JSX, you might also needimport React from 'react';depending on your Babel configuration, but oftenuseStatealone is enough if React is implicitly available. - Why it works: This makes the
useStatehook available in your component’s scope, allowing you to call it.
- Diagnosis: Look at the top of your component file. Do you see
-
Importing
Reactas a Default Import and Using Named Import: Sometimes, people importReactas a default and then try to useuseStateas if it were a property of that default.- Diagnosis: Check for imports like
import MyReactAlias from 'react';followed by an attempt to useMyReactAlias.useState(...). - Fix: Change your import to
import { useState } from 'react';. If you need other parts of React, you can combine them:import React, { useState } from 'react';. - Why it works:
useStateis a named export from thereactpackage, not a property of the default export.
- Diagnosis: Check for imports like
-
Conflicting File or Directory Names: You might have accidentally created a file named
react.jsor a directory namedreactin your project’ssrcfolder (or a parent directory). Node.js’s module resolution might pick this up instead of the actualnode_modules/reactpackage.- Diagnosis: Manually inspect your
srcdirectory for any files or folders namedreact.js,react/index.js, or justreact. - Fix: Rename any conflicting files or directories. For example, rename
src/react.jstosrc/myReactUtils.js. - Why it works: This prevents your local files from shadowing the actual
reactpackage installed innode_modules, ensuring thatimport { useState } from 'react';correctly points to the library.
- Diagnosis: Manually inspect your
-
Typo in
useState: A simple typographical error can cause this.- Diagnosis: Carefully check the spelling of
useStatein your code. Is ituserState,usestate,useState(with a lowercase 's')? - Fix: Correct the spelling to
useState. - Why it works: JavaScript is case-sensitive.
useStateis the correct name; anything else is an undefined variable.
- Diagnosis: Carefully check the spelling of
-
Using
useStateOutside a Functional Component or Custom Hook: Hooks can only be called at the top level of React functional components or custom hooks. CallinguseStateinside a regular JavaScript function, a class component, or within conditional logic (likeifstatements or loops) will result in errors, though this specific error message is less common for this particular mistake than "Rendered fewer hooks than expected". However, ifuseStateisn’t properly imported within that scope, it could manifest as "not a function."- Diagnosis: Trace where
useStateis being called. Is it directly inside yourfunction MyComponent() { ... }orconst MyComponent = () => { ... }? Or is it inside a nested function, a callback, or auseEffect’s cleanup function where it’s not supposed to be? - Fix: Ensure
useStateis called only at the top level of your functional component or custom hook. If you need state in a different context, consider passing state down as props or using context API. - Why it works: React’s hook rules ensure that hooks are called in a consistent order during rendering, which is only possible at the top level of components/hooks.
- Diagnosis: Trace where
-
Corrupted
node_modulesor Incomplete Installation: Sometimes, thereactpackage in yournode_modulesmight be corrupted, or the installation process might have failed.- Diagnosis: Delete your
node_modulesfolder andpackage-lock.json(oryarn.lock) file, then runnpm install(oryarn install) again. Check the output for any installation errors related toreact. - Fix: Reinstall dependencies:
rm -rf node_modules package-lock.json && npm install(orrm -rf node_modules yarn.lock && yarn install). - Why it works: This ensures you have a clean, complete, and correctly installed version of the
reactpackage.
- Diagnosis: Delete your
-
Using an Older Version of React: If you’re working with a very old project that hasn’t been updated, you might be using a version of React that doesn’t support hooks, or where
useStatewas introduced differently. However, this is highly unlikely for the "is not a function" error specifically, asuseStatehas been part of the public API since React 16.8.- Diagnosis: Check your
package.jsonfor thereactversion. - Fix: Upgrade React to a version that supports hooks (16.8+). This often involves updating other dependencies as well.
- Why it works: Ensures compatibility with the modern React API that includes hooks.
- Diagnosis: Check your
After fixing the above, you might encounter a TypeError: X is undefined if you try to use a variable that wasn’t correctly imported or declared.