how do I set a cookie in next js in a server component and read it in a client component
Unanswered
Oriental posted this in #help-forum
OrientalOP
"use server"
//imports
import { cookies } from 'next/headers'
import UIPage from "@/components/ui/...{removed for privacy}";
export default async function UIPage({ params }: { params: { id: string }}): Promise<JSX.Element> {
cookies().set('OptionIds', "Jeff");
return (
<div>
<NarrowOptionsPage />
</div>
)
}
//imports
import { cookies } from 'next/headers'
import UIPage from "@/components/ui/...{removed for privacy}";
export default async function UIPage({ params }: { params: { id: string }}): Promise<JSX.Element> {
cookies().set('OptionIds', "Jeff");
return (
<div>
<NarrowOptionsPage />
</div>
)
}
1 Reply
OrientalOP
I get this error:
Error: Cookies can only be modified in a Server Action or Route Handler. Read more: https://nextjs.org/docs/app/api-reference/functions/cookies#cookiessetname-value-options
Error: Cookies can only be modified in a Server Action or Route Handler. Read more: https://nextjs.org/docs/app/api-reference/functions/cookies#cookiessetname-value-options