-
Notifications
You must be signed in to change notification settings - Fork 27.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve errors caused by client hook called in a server component (#4…
…4538) Improve errors caused by client hook called in a server component. Before  After  Fixes NEXT-311 ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
- Loading branch information
1 parent
90b86be
commit d89bdb2
Showing
5 changed files
with
314 additions
and
106 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
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,40 @@ | ||
# React client hook in Server Component | ||
|
||
#### Why This Error Occurred | ||
|
||
You are using a React client hook in a Server Component. | ||
|
||
#### Possible Ways to Fix It | ||
|
||
Mark the component using the hook as a Client Component by adding `'use client'` at the top of the file. | ||
|
||
##### Before | ||
|
||
```jsx | ||
import { useEffect } from 'react' | ||
|
||
export default function Page() { | ||
useEffect(() => { | ||
console.log('in useEffect') | ||
}) | ||
return <p>Hello world</p> | ||
} | ||
``` | ||
|
||
##### After | ||
|
||
```jsx | ||
'use client' | ||
import { useEffect } from 'react' | ||
|
||
export default function Page() { | ||
useEffect(() => { | ||
console.log('in useEffect') | ||
}) | ||
return <p>Hello world</p> | ||
} | ||
``` | ||
|
||
### Useful Links | ||
|
||
[Server and Client Components](https://beta.nextjs.org/docs/rendering/server-and-client-components) |
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,12 +1,41 @@ | ||
const invalidServerComponentReactHooks = [ | ||
'useDeferredValue', | ||
'useEffect', | ||
'useImperativeHandle', | ||
'useInsertionEffect', | ||
'useLayoutEffect', | ||
'useReducer', | ||
'useRef', | ||
'useState', | ||
'useSyncExternalStore', | ||
'useTransition', | ||
] | ||
|
||
function setMessage(error: Error, message: string): void { | ||
error.message = message | ||
if (error.stack) { | ||
const lines = error.stack.split('\n') | ||
lines[0] = message | ||
error.stack = lines.join('\n') | ||
} | ||
} | ||
|
||
export function formatServerError(error: Error): void { | ||
if (error.message.includes('createContext is not a function')) { | ||
const message = | ||
setMessage( | ||
error, | ||
'createContext only works in Client Components. Add the "use client" directive at the top of the file to use it. Read more: https://nextjs.org/docs/messages/context-in-server-component' | ||
error.message = message | ||
if (error.stack) { | ||
const lines = error.stack.split('\n') | ||
lines[0] = message | ||
error.stack = lines.join('\n') | ||
) | ||
return | ||
} | ||
|
||
for (const clientHook of invalidServerComponentReactHooks) { | ||
if (error.message.includes(`${clientHook} is not a function`)) { | ||
setMessage( | ||
error, | ||
`${clientHook} only works in Client Components. Add the "use client" directive at the top of the file to use it. Read more: https://nextjs.org/docs/messages/react-client-hook-in-server-component` | ||
) | ||
return | ||
} | ||
} | ||
} |
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.