Today, I encountered the following error message while developing React:

Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:

  1. You might have mismatching versions of React and the renderer (such as React DOM)
  2. You might be breaking the Rules of Hooks
  3. You might have more than one copy of React in the same app See fb. Me /react-inval… for tips about how to debug and fix this problem.



This error output has alerted me to the error code in my application myapp.jsx:

The const initializer was written outside of function:

The error disappears after moving it inside:



For more of Jerry’s original articles, please follow the public account “Wang Zixi “: