forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
unstable_useFlushEffects
hook (vercel#34117)
Implements vercel#30997 with some minor tweaks to the design: * The hook is moved to Client Components (e.g. `pages/_app` instead of `pages/_document`). This was a silly oversight in the original design: the hook needs to be called during server prerendering. * `useFlushEffects` instead of `useFlushEffect` as there isn't a particularly safe way to implement the singular semantics as a Client Component hook given the current implementation of server rendering. --- Fixes vercel#30997
- Loading branch information
Showing
15 changed files
with
492 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# `useFlushEffects` can not be called on the client | ||
|
||
#### Why This Error Occurred | ||
|
||
The `useFlushEffects` hook was executed while rendering a component on the client, or in another unsupported environment. | ||
|
||
#### Possible Ways to Fix It | ||
|
||
The `useFlushEffects` hook can only be called while _server rendering a client component_. As a best practice, we recommend creating a wrapper hook: | ||
|
||
```jsx | ||
// lib/use-style-libraries.js | ||
import { useFlushEffects } from 'next/streaming' | ||
|
||
export default function useStyleLibraries() { | ||
if (typeof window === 'undefined') { | ||
// eslint-disable-next-line react-hooks/rules-of-hooks | ||
useFlushEffects([ | ||
/* ... */ | ||
]) | ||
} | ||
/* ... */ | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# The `useFlushEffects` hook cannot be used more than once. | ||
|
||
#### Why This Error Occurred | ||
|
||
The `useFlushEffects` hook is being used more than once while a page is being rendered. | ||
|
||
#### Possible Ways to Fix It | ||
|
||
The `useFlushEffects` hook should only be called inside the body of the `pages/_app` component, before returning any `<Suspense>` boundaries. Restructure your application to prevent extraneous calls. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { useRefreshRoot as unstable_useRefreshRoot } from './refresh' | ||
export { useWebVitalsReport as unstable_useWebVitalsReport } from './vitals' | ||
export { useFlushEffects as unstable_useFlushEffects } from '../../shared/lib/flush-effects' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.