Next.js Discord

Discord Forum

Having trouble setting state from API call in NextJS 14

Unanswered
Nile Crocodile posted this in #help-forum
Open in Discord
Nile CrocodileOP
I am fetching data from supabase, but after successfully fetching data. I am unable to set the state to the new fetched data. What am i not understanding?

"use client";

import { supabase } from "@/utils/supabase/supabaseClient";
import { useState, useEffect } from "react";
import { Button } from "@/components/ui/button";

export default function Home() {
  const [top5projects, setTop5Projects] = useState([]);

  useEffect(() => {
    const fetchData = async () => {
      let { data: projects, error } = await supabase
        .from("projects")
        .select("*")
        .order("score", { ascending: false })
        .range(0, 5);

      if (error) console.error("error", error);

      setTop5Projects(projects);
      // console.log(projects);
    };

    fetchData();
    console.log(top5projects);
  }, []);

  return (
    <main className="flex flex-col items-center min-h-screen gap-4 p-6">
      <h1>Admin Panel</h1>
    </main>
  );
}

34 Replies

Nile CrocodileOP
console.log(top5projects); prints []
while
console.log(projects); prints [{...},{...},{...}]
@Jboncz Ive never used supabase... but are you fetching the data client side? O.o
Nile CrocodileOP
yes im fetching on client side
noice, so the problem is solved. right?
Nile CrocodileOP
this was my funny solution
const [top5projects, setTop5Projects] = useState([]);

  useEffect(() => {
    const fetchData = async () => {
      let { data: projects, error } = await supabase
        .from("projects")
        .select("*")
        .order("score", { ascending: false })
        .range(0, 5);

      if (error) console.error("error", error);

      setTop5Projects(projects);
    };

    fetchData();
  }, []);

  useEffect(() => {
    console.log(top5projects);
  }, [top5projects]);
i had to use 2 useEffects....
shitty code, but it works :sunglasses_1:. kidding btw
Nile CrocodileOP
bc it would console.log(projects) before it existed fully
im curious is there a more optimized way?
i also dont think youre allowed to make useEffect async and await setTop5Projects(projects);
hmmm... I can't really think of another way lmao
you can use .then tho right?
nvm
ignore me
Nile CrocodileOP
thanks for the help tho @averydelusionalperson
this issue is just a client thing 😂
@averydelusionalperson *is that sarcasm I detect*
Nile CrocodileOP
no
@Nile Crocodile this issue is just a client thing 😂
I wundah why you fetch on client tho
@averydelusionalperson I wundah why you fetch on client tho
Nile CrocodileOP
im doing a hackathon and i just started with next
i know youre suppose to fetch data use server component
but im use to react 🗿
im just hacking it together rn
make it work mentality
Just be careful doing that as you could be exposing secrets
yea
Also to answer your question everything in a userffect hook gets submitted to the dom once it finishes so for all intents and purposes the variable was null till it finished, you should worry about logging the state unless just for debugging and if your debugging just add a button to output the state. Just console.log whatever your setting the state to and rest assured react will handle it.
Also @averydelusionalperson don’t tell on me. 😂