Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why my react-query request data won't show?

This is my first time using react-query and I having problem request data from API.

This is the code:

import { useQuery } from "@tanstack/react-query";
import axios from "axios";
import "./App.css";

function App() {

  const fetchData = async () =>{
    const { data } = await axios.get("https://api.jikan.moe/v4/anime");
    const response = data.data;
    return response
  }

  const { isLoading,data,isError } = useQuery(
    ["posts"],
    fetchData,
      {
        retry: false,
        refreshInterval: 0,
        staleTime: 0
      }
  );
  if (isLoading) return <h1>...Loading</h1>;
  if (isError) return <h1>Fail</h1>;
    
  console.log(data)

  return (
    <div className="App">
      {data.map((datas)=>{
        <div>
          {datas.title}
        </div>
      })}
    </div>
  );
}

export default App;

when I console.log(data) it does show the list of data Array of 25 object element.
But when I try the use data.map to show the data in the website nothing appear but It does appear if I only try the show one of the array like

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{data[0].title}

the data does appear but when I try the show all array element with data.map but nothing seems to appear. Can someone help me?

thanks

>Solution :

the data does appear but when I try the show all array element with data.map but nothing seems to appear. Can someone help me?

Your Array#map does not return anything.

 {data.map((datas) => {
    return ( // return the JSX
      <div>
        {datas.title}
      </div>
    );
 })}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading