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

feat: Add “use client” to fix React Server Components #2656

Merged
merged 2 commits into from
Nov 21, 2023

Conversation

sawyerh
Copy link
Contributor

@sawyerh sawyerh commented Nov 10, 2023

Summary

This adds the "use client" directive to the transpiled JS module to support apps utilizing React Server Components:

Some context from the React docs on this directive:

When a file marked with 'use client' is imported from a Server Component, compatible bundlers will treat the module import as a boundary between server-run and client-run code.

For apps that use React Server Components, the app is server-rendered by default. 'use client' introduces a server-client boundary in the module dependency tree, effectively creating a subtree of Client modules.

Without this directive, apps attempting to use React Server Components are greeted with an error screen. See #2540.

I think this PR would be highly impactful to Next.js applications in particular, supporting the adoption of the new App Router and newer React features.

Additional context

I don't think this is the perfect solution to supporting React Server Components, but I think it's a quick fix. Longer term, #192 is likely a better approach, so that components without any React hooks or event listeners can be a server component.

Related Issues or PRs

How To Test

  1. Clone https://github.com/sawyerh/react-uswds-server-components-bug
  2. npm install
  3. npm run dev
  4. Open http://localhost:3000/react-uswds and observe an error
  5. Stop the dev server, then run: npm link @trussworks/react-uswds --save
  6. Open http://localhost:3000/react-uswds and observe the error is gone

Screenshots

CleanShot 2023-11-09 at 17 44 42@2x

@sawyerh sawyerh requested a review from a team as a code owner November 10, 2023 01:41
@sawyerh
Copy link
Contributor Author

sawyerh commented Nov 18, 2023

@brandonlenz Just checking in to see if this is a change y'all would be open to or if I should pursue an alternative. (Tagging you since you're listed in the readme as the active maintainer).

@brandonlenz brandonlenz changed the title Add “use client” to fix React Server Components feat: Add “use client” to fix React Server Components Nov 20, 2023
Copy link
Contributor

@brandonlenz brandonlenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am good with this change. Looks like a quick win

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

Successfully merging this pull request may close these issues.

2 participants