Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hooks can only be called inside the body of a function component #35

Closed
rankun203 opened this issue Nov 26, 2018 · 5 comments
Closed

Hooks can only be called inside the body of a function component #35

rankun203 opened this issue Nov 26, 2018 · 5 comments

Comments

@rankun203
Copy link

rankun203 commented Nov 26, 2018

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.

@rankun203 rankun203 changed the title Calling from a function component causes: Hooks can only be called inside the body of a function component Hooks can only be called inside the body of a function component Nov 26, 2018
@rankun203
Copy link
Author

rankun203 commented Nov 26, 2018

Thanks, I'll try to cold it.

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

@aalpgiray
Copy link

aalpgiray commented Dec 3, 2018

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. facebook/react#13991
I solved it by adding
"alias": { "react": "./node_modules/react" }
to the package.json

@doasync
Copy link

doasync commented Dec 22, 2018

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: facebook/react#14454 (comment)

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

@aalpgiray
Copy link

aalpgiray commented Dec 22, 2018

Thanks @doasync

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants