Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 677
Use @react-aria/ssr for isomorphic ID generation.#1409
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
f05353e98326a7c614ea613fe2da60a89762ab4728838b03525b9c12623db6c6aa3eb88f85aea841ad7fFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@primer/components': minor | ||
| --- | ||
| Use @react-aria/ssr for isomorphic ID generation. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| --- | ||
| title: Server-side rendering with Primer React | ||
| --- | ||
| ## SSR-safe ID generation | ||
| Some Primer components generate their own DOM IDs. Those IDs must be isomorphic (so that server-side rendering and client-side rendering yield the same ID, avoiding hydration issues) and unique across the DOM. We use [@react-aria/ssr](https://react-spectrum.adobe.com/react-aria/ssr.html) to generate those IDs. In client-only rendering, this doesn't require any additional work. In SSR contexts, you must wrap your application with at least one `SSRProvider`: | ||
| ``` | ||
| import {SSRProvider} from '@primer/components'; | ||
| function App() { | ||
| return ( | ||
| <SSRProvider> | ||
| <MyApplication /> | ||
| </SSRProvider> | ||
| ) | ||
| } | ||
| ``` | ||
| `SSRProvider` maintains the context necessary to ensure IDs are consistent. In cases where some parts of the react tree are rendered asynchronously, you should wrap an additional `SSRProvider` around the conditionally rendered elements: | ||
| ``` | ||
| function MyApplication() { | ||
| const [dataA] = useAsyncData('a'); | ||
| const [dataB] = useAsyncData('b'); | ||
| return <> | ||
| <SSRProvider> | ||
| {dataA && <MyComponentA data={dataA} />} | ||
| </SSRProvider> | ||
| <SSRProvider> | ||
| {dataB && <MyComponentB data={dataB} />} | ||
| </SSRProvider> | ||
| </> | ||
| } | ||
| ``` | ||
| This will ensure that the IDs are consistent for any sequencing of `dataA` and `dataB` being resolved. | ||
| See also [React Aria's server side rendering documentation](https://react-spectrum.adobe.com/react-aria/ssr.html). | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for writing docs! 💖
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add this doc to the side nav? https://github.com/primer/react/blob/main/docs/src/%40primer/gatsby-theme-doctocat/nav.yml