Next.js Discord

Discord Forum

Get data from server action inside client component

Unanswered
neon posted this in #help-forum
Open in Discord
'use server';

import { cookies } from 'next/headers';

export async function getCookie(key: string) {
  return cookies().get(key);
}

This is a very simple server action, retrive the cookie and return the value. However, how do I get the value in a client component, which cannot be asynchronous? Here's what I mean:
'use client'

export default function Home() {
  const value = getCookie('renderStars')
}

Here, value is just a pending promise, as the server action is asynchronous (which is required as per the docs)

How do I get the actual value of the cookie?

41 Replies

Make a new actions.ts file and call a that function from your client code.
@Param san Make a new actions.ts file and call a that function from your client code.
What? It's still gonna be async function, right?
for example, page.tsx or layout.tsx
they aren't meant to be client components
page.tsx is actually a client component...
am I doing something wrong?
its an antipattern, create a new file with client component and import it to page.tsx instead
oh, sounds good, let me try
is there like a pattern for the name of the new file?
@aardani its an antipattern, create a new file with client component and import it to page.tsx instead
Hey, so I get this error:
yeah see if you need cookies then why not set that route as a dynamically computed route at request time
remove dynamic = 'error'
@aardani remove `dynamic = 'error'`
from where?
check /layout.tsx or /page.tsx
@aardani check `/layout.tsx` or `/page.tsx`
Didn't see anything there, but I was able to get it to work by removing output: 'export' from next.config.js
but that option is required as the site is hosted on github pages
oh i see
why dint you say so at the first message
Sorry about that ;)
@aardani use useEffect to call server action then
What about using a setCookie function that's called inside a onClick event?
I can't seem to use useEffect inside another function
im not sure about setting cookie inside a client. Afaik, cookies are set in the server that is sent to the browser
So my whole idea of using a cookie to store user preference is wrong... what else do i use then?
well, when user preference is changed, you send a request to the server and the server stores it in a db and then returns the preference to the client via cookies
not from the client afaik
@aardani well, when user preference is changed, you send a request to the server and the server stores it in a db and then returns the preference to the client via cookies
okay, so where do I define that logic? In layout or page.tsx? or in a completely different file?
@neon okay, so where do I define that logic? In layout or page.tsx? or in a completely different file?
seeing that your export needs to be static, i suppose youa re arleady on the correct track by using Server Actions
just create a server action to setCookie to something
and then inside setCookie(){ ... }, use cookies().set() something
@aardani just create a server action to setCookie to something
so a server action to call a server action?
no
cookies() are not server action
its a dynamic function
and i dont even know if server action works in Github pages bro
how would that even works