experimental_useOptimistic only works in Client Components
Unanswered
Spectacled bear posted this in #help-forum
Spectacled bearOP
Hi there,
I want to know if someone else is having issues with the new react-hook
I imported it in a client component, but the terminal says:
But I'm using it in a client component so I don't understand. I use the last Nextjs and React version so that's weird:
I want to know if someone else is having issues with the new react-hook
experimental_useOptimistic. I imported it in a client component, but the terminal says:
experimental_useOptimistic only works in Client Components. Add the "use client" directive at the top of the file to use itBut I'm using it in a client component so I don't understand. I use the last Nextjs and React version so that's weird:
"next": "latest",
"react": "latest",
19 Replies
Spectacled bearOP
'use client'
import React, { experimental_useOptimistic as useOptimistic, useTransition } from "react";
import { Conversation, Message } from "@/app/actions/conversations";
import { MessageList } from "./MessageList";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { AiOutlineSend } from "react-icons/ai";
export default function Chat({
conversation,
sendMessage,
}: {
conversation: Conversation;
sendMessage: (message: Message) => Promise<void>;
}) {
const [disabled, startTransition] = useTransition();
const [optimisticMessages, addOptimisticMessage] = useOptimistic<Message[], Message>(
conversation.messages || [],
(state, newMsg) => [...state, newMsg],
);
async function formAction(formData: FormData) {
const newMsg: Message = {
user_message: formData.get("user_message") as string,
llm_response: "...",
};
startTransition(async () => {
await sendMessage(newMsg)
})
}
return (
<div className="items-center gap-5 w-full h-full flex flex-col px-14 pb-5">
{conversation.messages.length == 0 && (
<div className="bg-gradient-to-r from-[#FEA326] to-[#C40317] text-transparent bg-clip-text text-[50px] m-auto text-center w-[200px]">
Millie
</div>
)}
<div className="overflow-auto w-full h-full">
<MessageList messages={conversation.messages} />
</div>
<form
action={formAction}
className="flex w-full items-center space-x-2">
<Input name="user_message" type="text" placeholder="Send a request" />
<Button disabled={disabled} type="submit"><AiOutlineSend/></Button>
</form>
</div>
);
}Spectacled bearOP
@B33fb0n3 the hook is only available in the Canary version. Install the canary and you are good to go
nextjs should automatically use react canary in the app router
as for the original question, i have no idea what's wrong, sorry. though perhaps a reproduction repository for us to clone and test would help. the code and the versions you posted are all good to me
Spectacled bearOP
It doesn't work, it does let me import it, but when I go to a page that use my Chat component I get this error I sent. I'm going to try to delete node_modules and see if
i cant even import experimental_useOptimistic
share minimum reproducible repository, share it on github
Spectacled bearOP
O.o you're right, if I import useOptimistic it works perfectly even if I get an error
I don' tget the error anymore, now I get this one
@Spectacled bear Click to see attachment
seems like theres something wrong with the version then
particularly
@types/react