2

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 web site nothing appear but It does appear if I only try the show one of the array like

{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?

1 Answer 1

3

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>
    );
 })}
Sign up to request clarification or add additional context in comments.

3 Comments

oh yeah, thanks. But is this a react-query thing? cause I don't remember that I have to return anything when using normal useEffect fetch.
@JoshLee Nop, it's just related with the syntax. Nothing to do with react-query :)
oh ok, thanks mate appreciate it for your help. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.