Get data from server action inside client component
Unanswered
neon posted this in #help-forum
neonOP
'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.
neonOP
What? It's still gonna be async function, right?
@neon ts
'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:
tsx
'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?
why not just use cookies() and pass the cookie down to the client component from server component instead of whatever gynastic roundabouts you are trying to do
@aardani why not just use cookies() and pass the cookie down to the client component from server component instead of whatever gynastic roundabouts you are trying to do
neonOP
What server component are we talking about here?
for example, page.tsx or layout.tsx
they aren't meant to be client components
neonOP
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
neonOP
oh, sounds good, let me try
is there like a pattern for the name of the new file?
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'`
neonOP
from where?
check
/layout.tsx or /page.tsx@aardani check `/layout.tsx` or `/page.tsx`
neonOP
Didn't see anything there, but I was able to get it to work by removing
output: 'export' from next.config.jsbut that option is required as the site is hosted on github pages
oh i see
why dint you say so at the first message
neonOP
Sorry about that ;)
@aardani use useEffect to call server action then
neonOP
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
neonOP
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
neonOP
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
neonOP
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