Next.js Discord

Discord Forum

is fetching data in client components like this a bad practice?

Answered
Pacific sand lance posted this in #help-forum
Open in Discord
Pacific sand lanceOP
or should I be fetching from api endpoints instead? see the below code:

 useEffect(() => {
    const fetchData = async () => {
      try {
        const userId = user.session?.id
        const fetchedUser = await prisma.user.findUnique({
          where: { id: userId },
        });
        setUser(fetchedUser);
      } catch (err) {
        setError(err);
      }
    };
    fetchData();
  }, []);
Answered by riský
you should use server components, and if not possible then server actions/ api endpoints to preserve the querying on sever
View full answer

3 Replies

uhh fetching from db on client is not good
you should use server components, and if not possible then server actions/ api endpoints to preserve the querying on sever
Answer
Pacific sand lanceOP
i thought so, thanks