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:
Problem
Hi all, I am new to react and I am trying to create a react library of components and I came across this problem because one of the components I am creating uses REACT HOOKS. Disclaimer: this is my first time creating an issue, so please bear with me. So I am trying to create an accordion component which toggles between these classes [code block] and [code block] to open and close. package.json [code block] webpack.config.js [code block] This is the accordion container: [code block] This is the accordion item that will live inside the container: [code block] Now inside of a create-react-app I import these components My library and the create-react-app are relative to each other and I am using [code block] [code block] I have followed all of these instructions and I still get the same error. Current behavior? [code block] Steps to reproduce - clone https://github.com/sethandleah/react-lib - clone https://github.com/sethandleah/myapp - [code block] - [code block] - [code block] - [code block] - [code block] - [code block] - [code block] Expected behavior - It should show a button with a "plus" svg sign and the words "Hello" and "World" respectively - Open devtools and go to elements - When clicking on the button the class [code block] should toggle To see the above, do the following: - Uncomment these lines at [code block] [code block] - The comment out these line, alse at [code block]: [code block] Versions of React, Browser / OS are affected by this
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Solution: 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:
>My library and the create-react-app are relative to each other and I am using npm link Did you read this part? https://reactjs.org/warnings/invalid-hook-call-warning.html#duplicate-react It directly addresses the `link` workflow: <img width="677" alt="Screen Shot 2019-04-04 at 09 28 47" src="https://user-images.githubusercontent.com/810438/55541034-118ffc00-56bc-11e9-9cc0-f04c0f7c3260.png">
Trust Score
9 verifications
- 1
>My library and the create-react-app are relative to each other and I am using n
>My library and the create-react-app are relative to each other and I am using npm link
- 2
https://reactjs.org/warnings/invalid-hook-call-warning.html#duplicate-react
https://reactjs.org/warnings/invalid-hook-call-warning.html#duplicate-react
- 3
It directly addresses the `link` workflow:
It directly addresses the `link` workflow:
- 4
<img width="677" alt="Screen Shot 2019-04-04 at 09 28 47" src="https://user-imag
<img width="677" alt="Screen Shot 2019-04-04 at 09 28 47" src="https://user-images.githubusercontent.com/810438/55541034-118ffc00-56bc-11e9-9cc0-f04c0f7c3260.png">
Validation
Resolved in facebook/react GitHub issue #15315. Community reactions: 144 upvotes.
Verification Summary
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep