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

Fix invalid hook usage for exports #4385

Merged
merged 3 commits into from
Aug 23, 2022

Conversation

krolebord
Copy link
Contributor

@krolebord krolebord commented Aug 19, 2022

Changes

Closes #4220

Fixes warning when using hooks inside the react components not exported as a function declaration

Adds proper support for react components exported as

// named arrow expression
export const X = () => {...}; 

function Y1 () {...}
const Y2 = () => {...};

// Export default
export default Y2;

// Export specifiers
export { Y1 as Comp, Y2 }

// Export default object
export default { Y1, Y2 }

Testing

Tested manually.
I would like to add tests if needed, just unsure of how to test for warnings in the build output

Docs

No changes in docs needed

@changeset-bot
Copy link

changeset-bot bot commented Aug 19, 2022

🦋 Changeset detected

Latest commit: d3f0fcc

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 13 packages
Name Type
astro Patch
@e2e/astro-component Patch
@e2e/error-react-spectrum Patch
@e2e/error-sass Patch
@e2e/errors Patch
@e2e/lit-component Patch
@e2e/preact-component Patch
@e2e/react-component Patch
@e2e/solid-component Patch
@e2e/solid-recurse Patch
@e2e/svelte-component Patch
@e2e/e2e-tailwindcss Patch
@e2e/ts-resolution Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Aug 19, 2022
@krolebord krolebord changed the title Fix/invalid hook usage for exports Fix invalid hook usage for exports Aug 19, 2022
@FredKSchott FredKSchott added the - P5: urgent Fix build-breaking bugs affecting most users, should be released ASAP (priority) label Aug 21, 2022
@matthewp
Copy link
Contributor

@krolebord Does this prevent a warning?

@krolebord
Copy link
Contributor Author

krolebord commented Aug 22, 2022

@krolebord Does this prevent a warning?

@matthewp It does, tested it on my project, and the build completed without any warnings

Before

▶ src/pages/meet-the-team.astro
Warning: 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 https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
Warning: 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 https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
  └─ /meet-the-team/index.html (+26ms)

After

▶ src/pages/meet-the-team.astro
  └─ /meet-the-team/index.html (+24ms)

@krolebord
Copy link
Contributor Author

Also added support for:

const X = () => {};
export default X;

And

function X() {}
export default X

@matthewp matthewp merged commit 8164fa6 into withastro:main Aug 23, 2022
@astrobot-houston astrobot-houston mentioned this pull request Aug 23, 2022
@krolebord krolebord deleted the fix/invalid-hook-usage-for-exports branch August 23, 2022 14:46
matthewp added a commit that referenced this pull request Aug 25, 2022
matthewp added a commit that referenced this pull request Aug 25, 2022
* Revert "Fix invalid hook usage for exports (#4385)"

This reverts commit 8164fa6.

* Adding a changeset
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P5: urgent Fix build-breaking bugs affecting most users, should be released ASAP (priority) pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid Hook Call in React
4 participants