How to resolve "TypeError: useOptimistic only works in Client Components" in Next.js with use client
Answered
Catla posted this in #help-forum
CatlaOP
I'm trying to use the useOptimistic hook in my Next.js page, and I've added the "use client" directive at the top of my file as suggested by the error message. However, I'm still encountering the error:
TypeError: useOptimistic only works in Client Components. Add the "use client" directive at the top of the file to use it.
Here's a snippet of my code:
'use client'
import { useOptimistic } from 'react'
import { send } from './actions'
type Message = {
message: string
}
export function Thread({ messages }: { messages: Message[] }) {
const [optimisticMessages, addOptimisticMessage] = useOptimistic<Message[]>(
messages,
(state: Message[], newMessage: string) => [
...state,
{ message: newMessage },
]
)
return (
<div>
{optimisticMessages.map((m, k) => (
<div key={k}>{m.message}</div>
))}
<form
action={async (formData: FormData) => {
const message = formData.get('message')
addOptimisticMessage(message)
await send(message)
}}
>
<input type="text" name="message" />
<button type="submit">Send</button>
</form>
</div>
)
}
"next": "^13.5.4"
I've read the documentation on Next.js, but I'm still facing this issue. Any insights on how to resolve this error would be greatly appreciated. Thanks!
TypeError: useOptimistic only works in Client Components. Add the "use client" directive at the top of the file to use it.
Here's a snippet of my code:
'use client'
import { useOptimistic } from 'react'
import { send } from './actions'
type Message = {
message: string
}
export function Thread({ messages }: { messages: Message[] }) {
const [optimisticMessages, addOptimisticMessage] = useOptimistic<Message[]>(
messages,
(state: Message[], newMessage: string) => [
...state,
{ message: newMessage },
]
)
return (
<div>
{optimisticMessages.map((m, k) => (
<div key={k}>{m.message}</div>
))}
<form
action={async (formData: FormData) => {
const message = formData.get('message')
addOptimisticMessage(message)
await send(message)
}}
>
<input type="text" name="message" />
<button type="submit">Send</button>
</form>
</div>
)
}
"next": "^13.5.4"
I've read the documentation on Next.js, but I'm still facing this issue. Any insights on how to resolve this error would be greatly appreciated. Thanks!
14 Replies
have you try this with the latest version of next?
CatlaOP
Yes, same error with v14
I don't get error
how do you render the <Thread /> component
CatlaOP
I tired to use it as a page but also as child component, how did you render in you case?
CatlaOP
Yes, pretty much the same here:
@Catla Yes, pretty much the same here:
oh you can't use form action with page router
CatlaOP
How did it work on your case? I see you have a form action too.
Which is under the page.tsx file (page router?)
you have to use app router which under the app folder
Answer
CatlaOP
Thanks! maybe the error message/docs could be improved, because it was not clear.
nextjs is big and have many feature, I think their doc is good enough