简体   繁体   English

react + mobx:我的 2 台机器中的 1 台出现“无效的钩子调用”

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

I've been following this Mobx tutorial:我一直在关注这个 Mobx 教程:

The code works fine on 1 of my 2 machines.该代码在我的 2 台机器中的 1 台上运行良好。 On the other machine, I get an error message when I run "npm start":在另一台机器上,当我运行“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.13.2(不适用于这个)
  • npm 8.11.0 (works on this machine) npm 8.11.0(适用于本机)

Both machines have the same version for the following:两台机器的以下版本相同:

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

The error goes away as long as I don't wrap the functions using observer .只要我不使用observer包装函数,错误就会消失。

Somehow things work fine after uninstalling and reinstalling mobx卸载并重新安装 mobx 后不知何故一切正常

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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