Unverified Commit 3c84b3ac authored by Adrian Bettridge-Wiese's avatar Adrian Bettridge-Wiese Committed by GitHub

Add useOptimistic to client-only errors (#53313)

### What?
This PR makes it so calling `experimental_useOptimistic` throws an error telling you it only works in a client component. Because the Next docs have an example of renaming it into `useOptimistic` in the import, I also added that as a forbidden import. There may be a better way to do this, if so, please let me know.

Fixes #53312 

### Why?
Currently, the error you get says `(0 , react__WEBPACK_IMPORTED_MODULE_1__.experimental_useOptimistic) is not a function or its return value is not iterable`. This is misleading.
<img width="1043" alt="Screenshot 2023-07-28 at 3 30 10 PM" src="https://github.com/vercel/next.js/assets/12662580/ee16fd84-633d-47a1-8db4-cfc050546614">

### How?
Adds `experimental_useOptimistic` to the lists of forbidden imports. Adds some specific tests around this, but I'm not sure they're necessary, looking at how the other tests are written.
Co-authored-by: 's avatarZack Tanner <1939140+ztanner@users.noreply.github.com>
parent c02dcd54
......@@ -9,6 +9,8 @@ const invalidServerComponentReactHooks = [
'useState',
'useSyncExternalStore',
'useTransition',
'experimental_useOptimistic',
'useOptimistic',
]
function setMessage(error: Error, message: string): void {
......@@ -52,7 +54,8 @@ ${addedMessage}`
}
for (const clientHook of invalidServerComponentReactHooks) {
if (error.message.includes(`${clientHook} is not a function`)) {
const regex = new RegExp(`\\b${clientHook}\\b.*is not a function`)
if (regex.test(error.message)) {
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`
......
......@@ -34,6 +34,8 @@ export const DISALLOWED_SERVER_REACT_APIS: string[] = [
'PureComponent',
'createContext',
'createFactory',
'experimental_useOptimistic',
'useOptimistic',
]
export const ALLOWED_PAGE_PROPS = ['params', 'searchParams']
......
......@@ -205,6 +205,76 @@ describe('Error Overlay for server components', () => {
await cleanup()
})
it('should show error when React.experiment_useOptimistic is called', async () => {
const { browser, cleanup } = await sandbox(
next,
new Map([
[
'app/page.js',
outdent`
import React from 'react'
export default function Page() {
const optimistic = React.experimental_useOptimistic()
return "Hello world"
}
`,
],
])
)
await check(async () => {
expect(
await browser
.waitForElementByCss('#nextjs__container_errors_desc')
.text()
).toContain(
'experimental_useOptimistic 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 'success'
}, 'success')
expect(next.cliOutput).toContain(
'experimental_useOptimistic 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'
)
await cleanup()
})
it('should show error when React.experiment_useOptimistic is renamed in destructuring', async () => {
const { browser, cleanup } = await sandbox(
next,
new Map([
[
'app/page.js',
outdent`
import { experimental_useOptimistic as useOptimistic } from 'react'
export default function Page() {
const optimistic = useOptimistic()
return "Hello world"
}
`,
],
])
)
await check(async () => {
expect(
await browser
.waitForElementByCss('#nextjs__container_errors_desc')
.text()
).toContain(
'experimental_useOptimistic 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 'success'
}, 'success')
expect(next.cliOutput).toContain(
'experimental_useOptimistic 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'
)
await cleanup()
})
it('should show error when React.<client-hook> is called in external package', async () => {
const { browser, cleanup } = await sandbox(
next,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment