Next.js Discord

Discord Forum

Problem with useEffect()

Answered
mayank posted this in #help-forum
Open in Discord
Avatar
so i am making a simple fetch api will get data from "https://dummyjson.com/products" the productlist page is working find but when i added a <Link href="/">home<Link> to go back to home page it goes back to it and shows a error -> "destroy is not a function " but when i reload the page the page renders smoothly without any error

code for productlist page is

"use client"

import { useEffect,useState } from "react"
import Link from "next/link";

export default function page() {
    const [product,setProduct] = useState([])
    const [loading,setLoading] = useState(true)

    useEffect(async() =>{
        try{
        let data = await fetch("https://dummyjson.com/products");
        data = await data.json();
        setProduct(data.products);
        setLoading(false)
        }catch(error){
            console.error(error);
            setLoading(false)
        }
    },[])
    if(loading){
        return <div>Loading...</div>;  // loading state is true when the component loads for first time and then it will be false after fetching all
    }
  return (
    <>
        <div>
           <center><h1>Productlist</h1></center>
           {
            product.map((items) => (
                <h3 key={items.id}>{items.title}</h3>
            ))
           }
           <Link href="/" as="/">home</Link>
        </div>
    </>
  )
}

please look into it i tried chatgpt and everyhting the issue is not resolving
Answered by Naeemgg
"use client";

import { useEffect, useState } from "react";
import Link from "next/link";

export default function Page() {
  const [product, setProduct] = useState([]);
  const [loading, setLoading] = useState(true);

 
  useEffect(()=>{async function fetchData(){
    try {
      let data = await fetch("https://dummyjson.com/products");
      data = await data.json();
      setProduct(data.products);
      setLoading(false);
    } catch (error) {
      console.error(error);
      setLoading(false);
    }}
    fetchData()
  }, []);
  if (loading) {
    return <div>Loading...</div>; // loading state is true when the component loads for first time and then it will be false after fetching all
  }
  return (
    <>
      <div>
        <center>
          <h1>Productlist</h1>
        </center>
        {product.map((items) => (
          <h3 key={items.id}>{items.title}</h3>
        ))}
        <Link href="/" as="/">
          home
        </Link>
      </div>
    </>
  );
}
View full answer

12 Replies

Avatar
"use client";

import { useEffect, useState } from "react";
import Link from "next/link";

export default function Page() {
  const [product, setProduct] = useState([]);
  const [loading, setLoading] = useState(true);

 
  useEffect(()=>{async function fetchData(){
    try {
      let data = await fetch("https://dummyjson.com/products");
      data = await data.json();
      setProduct(data.products);
      setLoading(false);
    } catch (error) {
      console.error(error);
      setLoading(false);
    }}
    fetchData()
  }, []);
  if (loading) {
    return <div>Loading...</div>; // loading state is true when the component loads for first time and then it will be false after fetching all
  }
  return (
    <>
      <div>
        <center>
          <h1>Productlist</h1>
        </center>
        {product.map((items) => (
          <h3 key={items.id}>{items.title}</h3>
        ))}
        <Link href="/" as="/">
          home
        </Link>
      </div>
    </>
  );
}
Answer
Avatar
Now it'd work fine
Avatar
ye now its working fine
like why was i getting a error ealier can you explain
@Naeemgg
Avatar
It is not recommended to utilize an async function directly in 'useEffect' like you did.
If you must, create an async function within it and invoke it immediately following the declaration, as I did.
Avatar
ok thanks
Avatar
You may rename this query "problem with useEffect" or something similar. simply because it is evident
Avatar
ok thanks bro @Naeemgg
i think i cannot rename this query
Avatar
click on 3 dots located at top right corner then "edit post"
Avatar
thanks