0
import React, { useRef, useEffect, useState } from "react";
import axios from 'axios';   
function App() {
    const  [memes,setMemes] = useState({});
    useEffect( ()=> {
    const fetchMemes  =     async () => { 
    await  axios.get("")
   .then (response => {
    setMemes({memes:   response.data.data.memes[(Math.floor(Math.random()*10))]})
     console.log(memes)
       })
     .catch(err => {
      console.log(err)
      })
  
      }

     fetchMemes()
  },[]) }

while fetching the api i am getting null response first response returns null response and after the second run or render it return the response : responses returned from the api

with getting this empty array in the first response I cannot perform array.map() as it is a null array please help solving this issue i've been trying many ways but none worked ☹

Nishant Jha
  • 11
  • 1
  • 2
  • 2
    you're not logging the response, you're logging `memes`, which is `{}` on first render and hasn't yet been updated when the first `console.log` runs. If you want to see teh response, just `console.log(response)` instead. (Or step through your code, or just check the response directly in the Network tab.) – Robin Zigmond Mar 06 '21 at 14:32
  • Does this answer your question? [setState doesn't update the state immediately](https://stackoverflow.com/questions/41278385/setstate-doesnt-update-the-state-immediately) – ggorlen Mar 06 '21 at 15:30
  • Thanks @Robin Zigmond , it tried your solution but that doesn't seems to work out , the api response that response i am trying to `setState` , by setting the state i want to perform .`map()` operation and if it returns null or undefined that sadly cannot be done – Nishant Jha Mar 08 '21 at 11:07

0 Answers0