Next.js Discord

Discord Forum

Response is not assigned to list

Answered
Mandarin fish posted this in #help-forum
Open in Discord
Mandarin fishOP
When I want to assign response to list, it fails and displays list undefined.
Here is the code
"use client";
import GetGuilds from "@/utils/homepage-fetch";
import { useState, useEffect } from "react";

export default function Search() {
const [list, setList] = useState();
let response;

const get = async () => {
response = await GetGuilds("r", 2, "programming");
setList(response);
};

useEffect(() => {
console.log(list);
}, []);
return (
<section className=' duration-100 absolute w-screen h-screen backdrop-blur-xl top-[92px] left-0 '>
<button onClick={() => get()}>23123</button>
<section></section>
</section>
);
}
Answered by Ray
ok so it is
const get = async () => {
    const response = await GetGuilds("r", 2, "programming");
    setList(response.guilds);
  };
View full answer

18 Replies

why not this?
  const get = async () => {
    const response = await GetGuilds("r", 2, "programming");
    setList(response);
  };
and remove let response;
also you need to make the useEffect run again when the list has changed by the following
 useEffect(() => {
    console.log(list);
  }, [list]);
Mandarin fishOP
Thanks but I don't know how to fix the fact that it thinks that list is not an array
const [list, setList] = useState([]);
Mandarin fishOP
it didn't help
what error you got now
<h1>{guild?.id}</h1>
Mandarin fishOP
you get this error after you click the button?
@Ray you get this error after you click the button?
Mandarin fishOP
yes
which mean the response is not an array
const get = async () => {
    const response = await GetGuilds("r", 2, "programming");
    console.log(repsonse)
    // setList(response);
  };
then check your console
@Ray which mean the response is not an array
Mandarin fishOP
ok so it is
const get = async () => {
    const response = await GetGuilds("r", 2, "programming");
    setList(response.guilds);
  };
Answer
Mandarin fishOP
dude, it's working!
thank you so much