-1

I get data from API but I cant set it to state, returns null.

    useEffect(async () => {
      await axiosClient.get(`/api/search`).then((res) => setQuery(res.data)).finally(() => setLoading(false));;
      console.log(query)
}, []); 

When i do console.log(res.data) It returns proper data but when I set it state returns null.

Fatih Can
  • 115
  • 8
  • Read the explanation for your case: https://betterprogramming.pub/why-dont-react-state-updates-reflect-immediately-9041c4377385 – Oleg May 09 '22 at 12:51
  • Try console log outside of your useEffect and the data will be visible in query. setState take some time to set the state and not do it immediately. – Ritik Banger May 09 '22 at 12:51

0 Answers0