React Hooks Error: Hooks can only be called inside the body of a function component

后端 未结 17 2132
旧巷少年郎
旧巷少年郎 2020-12-05 04:07

I am getting this error when using the useState hook. I have this in it\'s basic form, looking at the react docs for a reference, but am still getting this erro

17条回答
  •  误落风尘
    2020-12-05 04:10

    For fellow users of yarn workspaces, here's my situation and how I figured it out.

    • packages
      • foo
        • react@16.8.6
      • bar
        • react@16.10.1

    The Facebook docs on Invalid Hook Call Warning say nothing about yarn workspaces, so I assumed my config was correct. But it wasn't. You can fix the error only by using the same version across all your packages.

    In the example above, you have to bump the version of react from "foo" to 16.10.1, so that it matches the react version from "bar".

    Bonus: see this discussion on GitHub for a beautiful collection of emotional baggage offloaded on the Internet.

提交回复
热议问题