0

I am making a web application that fetches data from API using axios, and showes the data.
However, fetching API takes too long, so I used Loader component, and after the axios GET method is over, I wanted to show the fetched data.
But my problem is, it fetches with no errors, Loader works perfect, and after fetching is done, Loader goes off. But my mapped data does not render on web. How can I fix this problem?

  const [isLoading, setLoading] = useState(true);
  const [lunchData, setLunchData] = useState(mockServingData);

  useEffect(() => {
    axios.get(URL).then((response) => {
      setLunchData(response.data.menu[0].lunch);
      lunch = lunchData.lunch;
      setLoading(false);
    });
  }, []);
  

  if (isLoading) {
    return <div className="App"><Loader type="spin" color="RGB 값" message="Loading..." /></div>;
  }
  const lunchMenuList = lunch.map((menu, index) => (
    <li key={index}>{menu}</li>
  ));


  return (
    <div className="App">
      {lunchMenuList}
    </div>
  );
  • I used var lunch just in development process, because mockServingData is not in the perfect form.
  • `lunch = lunchData.lunch;` you can't access the updated state in the same render cycle, so this is just setting it to the current value. Either set `lunch` using the response `lunch = response.data.menu[0].lunch.lunch`, or just map `lunchData` directly, `const lunchMenuList = lunchData.lunch.map(...` – pilchard Jun 02 '22 at 15:32
  • Does this answer your question? [The useState set method is not reflecting a change immediately](https://stackoverflow.com/questions/54069253/the-usestate-set-method-is-not-reflecting-a-change-immediately) – pilchard Jun 02 '22 at 15:35
  • @plichard unfortunately, it isn't the one Im'm looking for – Friedrich Nietzsche Jun 02 '22 at 21:55
  • can you provide api response here? – Aman Sadhwani Jun 03 '22 at 04:23

0 Answers0