简体   繁体   中英

react + mobx: "Invalid hook call" in 1 of my 2 machines

I've been following this Mobx tutorial:

The code works fine on 1 of my 2 machines. On the other machine, I get an error message when I run "npm start":

    Warning: Invalid hook call. Hooks can only be called inside of the body of a function component.

    Uncaught TypeError: Cannot read properties of null (reading 'useState')
    at Object.useState (react.development.js:1622:1)
    at useObserver (useObserver.ts:29:1)
    at observerComponent (observer.ts:104:1)
    at renderWithHooks (react-dom.development.js:16305:1)
    at updateFunctionComponent (react-dom.development.js:19588:1)
    at updateSimpleMemoComponent (react-dom.development.js:19425:1)
    at updateMemoComponent (react-dom.development.js:19284:1)
    at beginWork (react-dom.development.js:21673:1)
    at HTMLUnknownElement.callCallback (react-dom.development.js:4164:1)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:4213:1)

Any idea why this is happening? The machines have almost identical setups...

  • npm 8.13.2 (doesn't work on this one)
  • npm 8.11.0 (works on this machine)

Both machines have the same version for the following:

  • react 18.2.0
  • mobx-react 7.5.2
  • mobx-react-lite 3.4

The error goes away as long as I don't wrap the functions using observer .

Somehow things work fine after uninstalling and reinstalling mobx

npm uninstall mobx-react
npm install mobx-react --save
npm audit fix

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM