Next.js Discord

Discord Forum

I am using use function of react in nextjs v13 and it give errors.

Unanswered
Huntaway posted this in #help-forum
Open in Discord
HuntawayOP
Unhandled Runtime Error
Error: async/await is not yet supported in Client Components, only Server Components. This error is often caused by accidentally adding `'use client'` to a module that was originally written for the server.

71 Replies

Original message was deleted
HuntawayOP
"use client";

import { use } from 'react';
import { getSessionData } from '@/lib/functions';

const newData = use(getSessionData());
European sprat
show the full code
@European sprat show the full code
HuntawayOP
this is full codes
functions.js
import { getServerSession } from 'next-auth/next';
import authOptions from "./authOptions";

export async function getSessionData() {
    const session = await getServerSession(authOptions);
    if (!session) return {
        data: null,
    };
    return {
        data: session.user.info,
    }
}
You can't use hooks outside of a React component
@idan Where is the component itself?
HuntawayOP
ok and what error does it give you?
the same error as before?
@idan ok and what error does it give you?
HuntawayOP
Unhandled Runtime Error
Error: async/await is not yet supported in Client Components, only Server Components. This error is often caused by accidentally adding 'use client' to a module that was originally written for the server.

Source
src/app/dashboard/page.js (12:24) @ getSessionData

  10 | 
  11 | export default function Dashboard() {
> 12 | const newData = use(getSessionData());
     |                    ^
  13 | console.log(newData);
  14 | const { data: session } = useSession();
  15 | let dataGuild;

Show collapsed frames
probably because you're using getSessionData which is a function that uses async.
HuntawayOP
without it how can we fetch
using the use hook.
HuntawayOP
hmmk
that's what i'm trying to tell you, you shouldn't use await or async in client components.
HuntawayOP
Unhandled Runtime Error
Error: async/await is not yet supported in Client Components, only Server Components. This error is often caused by accidentally adding `'use client'` to a module that was originally written for the server.

Source
src/lib/functions.js (6:25) @ getServerSession

  4 | 
  5 | export function getSessionData() {
> 6 | const session =  use(getServerSession(authOptions));
    |                     ^
  7 | if (!session) return {
  8 |     dataGuild: null,
  9 | };
Call Stack
getSessionData
src/app/dashboard/page.js (12:38)
:fine: hmm
do you have any other functions in that file that use async or await?
HuntawayOP
nop
export default function Dashboard() {
    const newData = use(getSessionData());
    console.log(newData);
    const { data: session } = useSession();
    let dataGuild;
    if (!session) {
        dataGuild = null;
    } else {
        dataGuild = session.user.guilds;
    }
see
i meant in the getSessionData function's file
are there any other functions?
European sprat
why aren't you using useSession() in the client component?
he probably wants to get the data using the server
useSession is for client side rendering
if you don't care, use it
European sprat
and his dashboard page is already a client component
@idan he probably wants to get the data using the server
HuntawayOP
yup because useSession give delay
@European sprat and his dashboard page is already a client component
a client component isn't client side rendering though
European sprat
i don't understand what you're getting at

all i'm saying is, his dashboard page is a client component so he should use the next-auth provided hook for sessions not try and make something up
HuntawayOP
that issue solved now
Unhandled Runtime Error
Error: Invariant: headers() expects to have requestAsyncStorage, none available.
Unhandled Runtime Error
Error: Invariant: headers() expects to have requestAsyncStorage, none available.

Source
src/lib/functions.js (6:42) @ authOptions

  4 | 
  5 | export async function getSessionData() {
> 6 | const session =  use(getServerSession(authOptions));
    |                                      ^
  7 | if (!session) return {
  8 |     dataGuild: null,
  9 | };
Call Stack
getSessionData
src/app/dashboard/page.js (12:34)
div
src/app/dashboard/page.js (111:25)
Show collapsed frames
European sprat
probably because you're still trying to use a server only function in a client component with an experimental hook
@European sprat i don't understand what you're getting at all i'm saying is, his dashboard page is a client component so he should use the next-auth provided hook for sessions not try and make something up
useSession should be used if you want the user to see loading states. getServerSession should be used if you want to load the user session using server-side rendering, which is helpful in cases where you don't want the user to experience delays.
European sprat
i'm completely aware of when to use getServerSession. i'm saying he's using it in a client component which is why this isn'tw orking
Yep, so basically, if you want to load the user's session using the server, you need to remove that 'use client' and make all your clickable buttons a separate component.
HuntawayOP
ik but i am confuses about it hmmm
why would you be confused? just create a separate client component and put there all your buttons or input fields
to components
just like you did before
@idan just like you did before
HuntawayOP
like this i need to make multiple compoenets
cuz dashboard have many pages
I know
that is how it works if you wanna use the app router.
you can always go back to the pages router
HuntawayOP
:this_is_fine: i am using app router for future ideas
and here i get stuck for this issue
you shouldn't get stuck though
HuntawayOP
like a time come where page route stop updates by devs
and we need to shift app
yeah i understand
i don't think it will happen anytime soon, but i understand what you're saying
HuntawayOP
hmm bro
: /
almost page done
i would just create a separated component for client stuff
HuntawayOP
but i am stuck this place
that's the only solution
HuntawayOP
i think they should make function for this
it's either that or you just use useSession and load the session in the client
HuntawayOP
so we can make to use it easy
@idan it's either that or you just use `useSession` and load the session in the client
HuntawayOP
ik but it make more complicated
yeah i understand, i build dashboards
i just create a separate component for client stuff, and it works flawlessly
it might seems like more work at first, but it makes things a lot more easier to read and maintain.
pro tip: use folders
HuntawayOP
@idan so basically i make components by idk why i can't get this dats in page props
import { getSessionData } from "@/lib/functions";
import DashboardMainComponent from "@/dashComponents/main";

export default async function Dashboard() {
    const data = await getSessionData();
    <DashboardMainComponent dataGuild={data.dataGuild} />
}