Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type Error : destroy is not a function in Reactjs while call api async function in useEffect hook


const [dailyData, setDailyData] = useState([]);
 useEffect(async () => {
    const fetchData =  await fetchDailyData();  // fetchDailyData() is calling Api 
    setDailyData(fetchData); 

    console.log(fetchData); //fetchData print the value but dailyData not updating


  },[]);

showing destroy is not a function and func.apply is not a function

like image 373
Naveen kumar Avatar asked Oct 26 '25 08:10

Naveen kumar


1 Answers

Effect hook callbacks can't be async. Instead, declare a callback function scoped async function and then invoke it. Also, as pointed out by @StéphaneVeyret your "async" effect hook callback implicitly returns a Promise which gets interpreted as an effect hook clean up function. It isn't one though and causes error.

useEffect(() => {
  const asyncFetchDailyData = async () => {
    const fetchData = await fetchDailyData(); // fetchDailyData() is calling Api 
    setDailyData(fetchData);
    console.log(fetchData);
  }

  asyncFetchDailyData();
}, []);
like image 124
Drew Reese Avatar answered Oct 28 '25 22:10

Drew Reese



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!