getting error "TypeError: Cannot read properties of null (reading 'useState')" on useState usage react
Asked Answered
A

17

49

So I'm trying to use useState in my nextjs app, but as soon as I add in the line of code to initialize useState it throws a popup error with the error message: TypeError: Cannot read properties of null (reading 'useState')

Here is a simplified version of my code:

import { useState } from 'react'

export default function Home() {
    return (
        <div>
            <Transactions />
        </div>
    )
}



const Transactions = () => {

    const [state, setState] = useState('test')
...
}

React/Next versions I'm running:

"next": "12.1.5",
"react": "18.0.0",
"react-dom": "18.0.0"

Screenshot of error from chrome dev tools: Screenshot of error from chrome dev tools

Any thoughts on what's causing this? Thanks

EDIT Very dumb bug on my end, issue was resolved by importing react...

Acidify answered 17/4, 2022 at 0:22 Comment(3)
I can't repro with the simplified version of your code, so there's a good chance the problem is in the details. Could you post a full example?Lobito
Repo link: github.com/bracesproul/Budget-Tracker/blob/main/pages/…Acidify
@BraceSproul I have this error and I AM importing react. I'm using the latest nextjs, react and typescript.Ignorance
A
19

Just define State inside the Home function

Achorn answered 18/6, 2022 at 7:13 Comment(3)
lol, I made this mistake. Thanks duh!Levina
Could you share the fixed code?Canoodle
can you help with a brief explanation as the nested component can also be having state.Sunny
P
18

If you are using Next 13 App router this could be happening if you are using an async function

Wrong:

"use client"

export default async function Page() {
    const [variable, setVariable] = useState();
}

Right:

"use client"

export default function Page() { //Remove Async Keyword
        const [variable, setVariable] = useState();
}
Pd answered 14/6, 2023 at 21:41 Comment(2)
What if I need the function to be async though?Recap
You need to use 'useEffect' to get the data and manage the page loading state using 'useState' @JackDamonPd
S
6

So I forked and cloned your github repo and was not able to reproduce the error on my system. One thing I found was in your navBar component you import

import Link from 'next/Link'

which threw me an error until I could change it next/link to lowercase

import Link from 'next/link'

What I would recommend doing is to reinstall react, just with a simple npm command to see if that fixes your problem. If not let me know!

npm uninstall react
npm install react@latest
Soso answered 18/4, 2022 at 1:49 Comment(0)
B
6

May you use "hook" outside of the function component because hook does not work outside of the function

Breviary answered 11/1, 2023 at 11:29 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Baumann
W
1

This can also happen because of the way you export the function:

wrong:

export default YourComponent();

correct:

export default YourComponent;
Wire answered 29/9, 2023 at 20:58 Comment(0)
B
0

Just create a folder called "hooks" add paste this code there. I was the same probleam and that worked for me

Burletta answered 7/10, 2022 at 2:59 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Baumann
R
0

Got the same error message and this line of code basically fixed it.

import React, { useState } from 'react';

Noticed that I had the incorrect import which was causing the issue on my end. Hope this helps.

Refectory answered 11/11, 2022 at 19:10 Comment(0)
D
0

Mostly, the error is because useState is not being used inside the function that's being exported.
Just use useState inside a function and the issue will be solved!

Demagogic answered 13/7, 2023 at 7:23 Comment(0)
M
0

use the function as

export default function Register () {
  const [credentials, setCredentials] = useState();

This work for you

Mccormick answered 23/8, 2023 at 5:25 Comment(0)
G
0

Had the error even after importing react,usestate and even defined the usestate inside a function, but the actual issue was I was calling the function in the export line removed the () error resolved, Hope this helps.

Galinagalindo answered 15/9, 2023 at 6:14 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Baumann
I
0

If you have defined const [userName, setUserName] = useState(""); outside the component. Than this error may come.

Just move your const [userName, setUserName] = useState(""); inside your component.

Not working:

const [userName, setUserName] = useState("");

const AppLayout = () => {
  return (
      <div className="app">
        <Header />
        <Body />
      </div>
  );
};

Working:



const AppLayout = () => {
const [userName, setUserName] = useState("");
  return (
      <div className="app">
        <Header />
        <Body />
      </div>
  );
};
Intermit answered 8/10, 2023 at 21:48 Comment(0)
W
0

For me, my /node_modules was corruped for some reason

rm -fr /node_modules
npm i
Wortman answered 24/4, 2024 at 22:15 Comment(0)
A
0

I got the same error, in my case it was because of a library (ReactToastify or simply 'toast') I used. After you install this library you have to write after the closing in App.js. Hope that helps.

Augustinaaugustine answered 11/5, 2024 at 1:30 Comment(0)
P
0

In my case, I had a git submodule in a subfolder. This submodule had its own node_modules. I had to remove them to fix this error.

Interestingly, this only occurred in the production build.

Plucky answered 30/5, 2024 at 9:21 Comment(0)
C
0

I was developing a Shopify app and had the same issue.

In my case, I deleted the react folder from my_project\node_modules\@shopify\cli-kit\node_modules\react and re-run the app. It re-created the react folder by its own and fixed the problem.

Charlatanry answered 5/6, 2024 at 19:49 Comment(0)
C
-3

When migrating to next make sure you don't use any other Link except from next-js.

Couscous answered 12/9, 2022 at 22:25 Comment(0)
C
-3

Just reinstall the newest version react

npm install react@latest
Ceaseless answered 2/10, 2022 at 8:37 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.