react icon indicating copy to clipboard operation
react copied to clipboard

Bug: Cannot read property 'useState' of null

Open haikyuu opened this issue 1 year ago • 29 comments

React version: 18.2

Steps To Reproduce

  1. Install a library that doesn't support React 18 (e.g [email protected])
  2. Try to use useState in a component

Link to code example: https://scrimba.com/scrim/co257466ea4774401eb77c8a6

Note that you can pause the scrim ("video") to edit code and see the result by yourself.

The current behavior

Crashes without a helpful error message

The expected behavior

Should display a helpful error message.

This issue was reported before but closed because of lack of reproduction code https://github.com/facebook/react/issues/24774 and https://github.com/facebook/react/issues/24658

haikyuu avatar Jul 14 '22 11:07 haikyuu

I have a bug with same error message, but all the libraries use react18. I'll describe an architecture and here is a link to an example. Can you take a look, does it relative to the issue?

Arch

So, I have react18, webpack5, umd. There are several projects that linked together as local delendencies.

I link projects using /dist folder. I'm not really sure about it, but I decided to use small exports instead of linking to /src to save hard drive space.

I'm not really sure how webpack-es6 tree-shaking works, but I decided to create a bundle per source file instead of creating bundle per project. Just, well, it seems that webpack will understand it, probably. So, there is no /dist/index.js file, but a lot of /dist/**/*.(js|d.ts) files to import.

Projects
  1. webpack - build-only utilites, builds as webpack.target = 'node', but I don't think they affect runtime.
  2. common-react - a library with react-specific functions and components. Builds as webpack.target = 'web'
  3. application - a library that renders react <App /> component. Depends on common-react, builds as webpack.target = 'web'

If you make a change in a project, be sure to build the project using yarn build and run yarn upd in target to install the new version.

Run

./bootstrap.sh # it will initialize and build all the projects
cd application
yarn start

Error

There's a react hook. When the hook imports from /application folder, it works fine. But when I import it from @snowinmars/common-react project, it fails with useObserver.ts?0fc0:122 Uncaught TypeError: Cannot read properties of null (reading 'useState')

  1. If I add externals: {react: 'react'} to /common-react/webpack.config.js, hook works. But it's a hack.
  2. There was a build misconfiguration, when /common-react/dist/hooks/useRequest.js bundled to 430Kb file due to it was binded with whole react. Now it doesn't - all exported files are pretty small.

So...here we are. If you need any further details - just ask)

snowinmars avatar Aug 07 '22 07:08 snowinmars

I too getting similar error, when I'm pointing my local library in react application.

"devDependencies": { "@emerald/reactmaterial": "file:../emerald.reactmaterial/dist/emerald/reactmaterial", } image

If Im install it from server, it works fine.

bigbunty avatar Aug 26 '22 14:08 bigbunty

This problem also occurred to me, stops package development completely. Need help!

rollsover avatar Sep 26 '22 10:09 rollsover

The problem is that when using local packages, npm link is used, which in turn copies node_modules (project/node_modules/package/node_modules), if they are deleted, the problem is solved.

rollsover avatar Sep 26 '22 10:09 rollsover

The problem is that when using local packages, npm link is used, which in turn copies node_modules (project/node_modules/package/node_modules), if they are deleted, the problem is solved.

hi! @rollsover Thank you for your method!But if delete a node_modules it will not be possible to continue development, can you tell me how you do it?

iDestin avatar Oct 11 '22 08:10 iDestin

The problem is that when using local packages, npm link is used, which in turn copies node_modules (project/node_modules/package/node_modules), if they are deleted, the problem is solved.

hi! @rollsover Thank you for your method!But if delete a node_modules it will not be possible to continue development, can you tell me how you do it?

To get rid of npm link you can also run something like npm uninstall library-name && npm install library-name --save before some release command , which will uninstall and install w/o any redundant node_modules inside the library-name package

Kiran554 avatar Oct 14 '22 20:10 Kiran554

Thanks for this @Kiran554 was driving me mad.

GGAlanSmithee avatar Nov 03 '22 14:11 GGAlanSmithee

The problem is that when using local packages, npm link is used, which in turn copies node_modules (project/node_modules/package/node_modules), if they are deleted, the problem is solved.

The workaround for me is to remove dependencies in the the package.json using prepack script and pack the library with npm pack and then install the output library-<version>.tgz file in the target project by npm install library-<version>.tgz

donus3 avatar Nov 12 '22 17:11 donus3

I could finally solve this issue with @rollsover 's workaround. Thank you!

The problem is that when using local packages, npm link is used, which in turn copies node_modules (project/node_modules/package/node_modules), if they are deleted, the problem is solved.

silkroadnomad avatar Dec 12 '22 08:12 silkroadnomad

I am facing the same issue with my package. Please help. I used only as simple as useState hooks in one place, and its showing the same error.
npm i @subhampanja/easycrm

subhampanja2 avatar Jan 08 '23 15:01 subhampanja2

I think this should be fixed if the corresponding dependencies set react as peer dependeny

The problem is that when using local packages, npm link is used, which in turn copies node_modules (project/node_modules/package/node_modules), if they are deleted, the problem is solved.

sarahsporck avatar Jan 17 '23 15:01 sarahsporck

The reason for this problem is that the react version in the project is inconsistent with the react version used in your package

If you use your package in a project and a certain version of react has been installed in the project. Then you install your package in this project, and a certain version of react is also installed in your package. Then you may get this error report

how to fix it?

You just need to put the react dependencies from your package into the peerDependencies

bad case

project

{
  "dependencies": {
    "react": "x.x.x",
    "react-dom": "x.x.x",
    "your package": "x.x.x"
  }
}

your package

{
  "dependencies": {
    "react": "x.x.x",
    "react-dom": "x.x.x"
  }
}

good case

project

{
  "dependencies": {
    "react": "x.x.x",
    "react-dom": "x.x.x",
    "your package": "x.x.x"
  }
}

your package

{
++  "peerDependencies": {
    "react": "x.x.x",
    "react-dom": "x.x.x"
  }
}

Maybe not right, but hopefully it will help you

https://nodejs.org/en/blog/npm/peer-dependencies/

iDestin avatar Mar 09 '23 12:03 iDestin

@iDestin , in my example react and react-dom versions are equal, but it causes the bug.

snowinmars avatar Mar 18 '23 20:03 snowinmars

Hi! For me this helped. React have some new rules that causes the app to crash.

In my case it was wrong way: useEffect(async ()=>{})

good way: useEffect(()=>{ async function fetchData() {} fetchData() })

Also with cleanup

wrong way: BackHandler.addEventListener('hardwareBackPress', backAction); **return BackHandler.**removeEventListener('hardwareBackPress');

good way: BackHandler.addEventListener('hardwareBackPress', backAction); return function cleanup() { BackHandler.removeEventListener('hardwareBackPress'); };

Good luck!

henaharon avatar Mar 20 '23 08:03 henaharon

THE SOLUTION OF THIS ISSUE IS THIS! :) -> https://iws.io/2022/invalid-hook-multiple-react-instances

OscarJVD avatar May 23 '23 03:05 OscarJVD

THE SOLUTION OF THIS ISSUE IS THIS! :) -> https://iws.io/2022/invalid-hook-multiple-react-instances

This issue is about useObserver.ts?0fc0:122 Uncaught TypeError: Cannot read properties of null (reading 'useState') bug. The article does not describe a fix. So it's not a solution - it's just related.

snowinmars avatar May 24 '23 21:05 snowinmars

Try npm uninstall library-name this command 1st and then npm install library-name --save. Hope this will work. <3

rahulnikam2002 avatar Jul 07 '23 06:07 rahulnikam2002

if the correspo

Didn't work for me.

ManuelGuerreroVana avatar Jul 12 '23 14:07 ManuelGuerreroVana

add this to tsconfig.json in your project. (if not work, delete react and react-dom in your package node_modules)

image

guaizi149 avatar Jul 14 '23 06:07 guaizi149

add this to tsconfig.json in your project. (if not work, delete react and react-dom in your package node_modules)

image

This worked like a charm!

imaaditya-stack avatar Jul 17 '23 11:07 imaaditya-stack