Easy-peasy: Hooks can only be called inside the body of a function component

Created on 26 Nov 2018  路  5Comments  路  Source: ctrlplusb/easy-peasy

My code is like:

// 1st useAction
import { useAction } from 'easy-peasy'

// 2nd useAction
// function useAction(mapActions) {
//   // Get store myself, I had to rebuild easy-peasy and export StoreContext
//   const store = React.useContext(peasy.StoreContext)
//   return mapActions(store.dispatch)
// }

export const View = () => {
  const updateDocForPrint = useAction(
    (dispatch: any) => dispatch.docForPrint.updateDocForPrint
  );
};

As you can see, View is a function component, however it still produces an error:

/app/node_modules/easy-peasy/node_modules/react/cjs/react.development.js:125
Uncaught Error: Hooks can only be called inside the body of a function component.
    at invariant (/app/node_modules/easy-peasy/node_modules/react/cjs/react.development.js:125)
    at resolveDispatcher (/app/node_modules/easy-peasy/node_modules/react/cjs/react.development.js:1424)
    at Object.useContext (/app/node_modules/easy-peasy/node_modules/react/cjs/react.development.js:1429)
    at Object.useAction (hooks.js:49)
    at exports.USBExplorerView.react_router_1.withRouter (eval at <anonymous> (store.ts:30), <anonymous>:18:37)
    at mountIndeterminateComponent (/app/node_modules/react-dom/cjs/react-dom.development.js:14620)
    at beginWork (/app/node_modules/react-dom/cjs/react-dom.development.js:15091)
    at performUnitOfWork (/app/node_modules/react-dom/cjs/react-dom.development.js:17817)
    at workLoop (/app/node_modules/react-dom/cjs/react-dom.development.js:17857)
    at HTMLUnknownElement.callCallback (/app/node_modules/react-dom/cjs/react-dom.development.js:149)

If I comment out 1st useAction and use 2nd useAction, a customized useAction, it will work.

I'm not sure if this is a React bug or an easy-peasy bug, the definition https://github.com/ctrlplusb/easy-peasy/blob/master/src/hooks.js#L48 is indeed a simple function, just like mine.

Most helpful comment

I solved this by removing the caret for React dependency in my package.json:

    "react": "16.7.0-alpha.2",
    "react-dom": "16.7.0-alpha.2",

See this comment for more info: https://github.com/facebook/react/issues/14454#issuecomment-449585005

@aalpgiray, you have "react": "^16.7.0-alpha.2" in your repo. Try to remove caret (to use exact version)

All 5 comments

Thanks, I'll try to cold it.

I'm closing this issue as answered, if the issue persists, I'll reopen it.

I am not using RHL, however, I am getting this error. I try to use other hooks but they work. It is a simple enough project. Here is the repo

Edit: This was the problem. https://github.com/facebook/react/issues/13991
I solved it by adding
"alias": { "react": "./node_modules/react" }
to the package.json

I solved this by removing the caret for React dependency in my package.json:

    "react": "16.7.0-alpha.2",
    "react-dom": "16.7.0-alpha.2",

See this comment for more info: https://github.com/facebook/react/issues/14454#issuecomment-449585005

@aalpgiray, you have "react": "^16.7.0-alpha.2" in your repo. Try to remove caret (to use exact version)

Thanks @doasync

Was this page helpful?
0 / 5 - 0 ratings

Related issues

giacomorebonato picture giacomorebonato  路  5Comments

andreiQuant picture andreiQuant  路  6Comments

24dev picture 24dev  路  5Comments

Bazooo picture Bazooo  路  4Comments

positimo picture positimo  路  3Comments