How to fetch data from backend inside 'use client'
Answered
Scaly-naped Pigeon posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Scaly-naped PigeonOP
is there anwyay to fetch data from backend without await inside a client side because I have to use useState and useEffect and it makes the code alot bigger is there anyway to fetch data without using useState and useEffect ?
const { data } = prisma.session.findMany()
Answered by James4u
then it's better to use useSWR rather than creating your own hook
38 Replies
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
@Scaly-naped Pigeon is there anwyay to fetch data from backend without await inside a client side because I have to use useState and useEffect and it makes the code alot bigger is there anyway to fetch data without using useState and useEffect ?
`const { data } = prisma.session.findMany()`
![Avatar](https://cdn.discordapp.com/avatars/970430299014041630/2af35fbc245502879f0c29d95f190040.webp?size=256)
then you need to hit your api route from you client component.
like inside your useEffect
like inside your useEffect
const { data } = prisma.session.findMany()
this is invalid ⬆️
![Avatar](https://cdn.discordapp.com/avatars/970430299014041630/2af35fbc245502879f0c29d95f190040.webp?size=256)
@James4u then you need to hit your api route from you client component.
like inside your useEffect
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Scaly-naped PigeonOP
yes inside server component i have to just await , is there a hook to help with this situation ?
![Avatar](https://cdn.discordapp.com/avatars/970430299014041630/2af35fbc245502879f0c29d95f190040.webp?size=256)
what's your problem indeed?
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Scaly-naped PigeonOP
my problem inside my client component , i cannot async await this so I can fetch the data , in client component i have to use useState and useEffect to make it work , is there an easier approach ?
well it's not a difficult approach but correct one
use other libraries like useSwr
it will help you to write down your own logic to handle loading state, error handling, revalidation ...
![Avatar](https://cdn.discordapp.com/avatars/970430299014041630/2af35fbc245502879f0c29d95f190040.webp?size=256)
@James4u use other libraries like useSwr
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Scaly-naped PigeonOP
so with useSWR , i can do this
useSWR(prisma.sessions.findMany()) ?
useSWR(prisma.sessions.findMany()) ?
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Scaly-naped PigeonOP
yea trying to use prisma in client component ^^
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
@Scaly-naped Pigeon yea trying to use prisma in client component ^^
![Avatar](https://cdn.discordapp.com/avatars/970430299014041630/2af35fbc245502879f0c29d95f190040.webp?size=256)
Prisma is Node.js ORM tool that is intended to be run on the server
![Avatar](https://cdn.discordapp.com/avatars/970430299014041630/2af35fbc245502879f0c29d95f190040.webp?size=256)
@James4u Prisma is Node.js ORM tool that is intended to be run on the server
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Scaly-naped PigeonOP
the only way to make it work is to use tanstack react query it works client and server side but i am using bun with elysia and tanstack is not supported to bun yet
![Avatar](https://cdn.discordapp.com/avatars/970430299014041630/2af35fbc245502879f0c29d95f190040.webp?size=256)
why don't you fetch data on the server component and pass down to your client component as prop?
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Scaly-naped PigeonOP
oh, is there an example to do this because i am using my client component inside a page.tsx
also it will provide types or i will have to add types to the props ?
![Avatar](https://cdn.discordapp.com/avatars/970430299014041630/2af35fbc245502879f0c29d95f190040.webp?size=256)
is your
page.tsx
a server component?![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Scaly-naped PigeonOP
its a useContext with a provider so I have to use client component but I am checking for user session so I have to use client component here is the full code
'use client';
'use client';
import React, { use, useContext } from 'react';
import api from '@/utils/eden';
import AuthContext from '@/utils/context';
export function AuthProvider({ children }: { children: React.ReactNode }) {
const { data, } = api.session.get()
return (
<AuthContext.Provider value={{ user : data?.user as any || null}}>
{children}
</AuthContext.Provider>
);
}
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Scaly-naped PigeonOP
do you mean that if i did this all my childrens will be a client components ?
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
@Scaly-naped Pigeon do you mean that if i did this all my childrens will be a client components ?
![Avatar](https://cdn.discordapp.com/avatars/970430299014041630/2af35fbc245502879f0c29d95f190040.webp?size=256)
CAN be a server component
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Scaly-naped PigeonOP
oh I see , should i use getServerSideProps for this ?
![Avatar](https://cdn.discordapp.com/avatars/970430299014041630/2af35fbc245502879f0c29d95f190040.webp?size=256)
are you using page router???
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Scaly-naped PigeonOP
I am using app router
![Avatar](https://cdn.discordapp.com/avatars/970430299014041630/2af35fbc245502879f0c29d95f190040.webp?size=256)
then there is no
getServerSideProps
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Scaly-naped PigeonOP
should I create a customHook for this and use useState and useEffect and preuse it instead ?
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
@Scaly-naped Pigeon should I create a customHook for this and use useState and useEffect and preuse it instead ?
![Avatar](https://cdn.discordapp.com/avatars/970430299014041630/2af35fbc245502879f0c29d95f190040.webp?size=256)
as I said you can't use prisma in the client side????
![Avatar](https://cdn.discordapp.com/avatars/970430299014041630/2af35fbc245502879f0c29d95f190040.webp?size=256)
@James4u as I said you can't use prisma in the client side????
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Scaly-naped PigeonOP
yea , I will use useState and useEffect for this , I though there was a better way to do this ^^
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
@Scaly-naped Pigeon yea , I will use useState and useEffect for this , I though there was a better way to do this ^^
![Avatar](https://cdn.discordapp.com/avatars/970430299014041630/2af35fbc245502879f0c29d95f190040.webp?size=256)
omg, did you understand what I mean?
you can't use prisma in the client side, so you need to create your api route
![Avatar](https://cdn.discordapp.com/avatars/970430299014041630/2af35fbc245502879f0c29d95f190040.webp?size=256)
then it's better to use useSWR rather than creating your own hook
Answer
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Scaly-naped PigeonOP
I see , will try to check if useSWR is compatibly with elysia js
appreciate the help man