React useQuery causes many rerenders before and after the data

Viewed 1625

I have this simple app where makes a call to an API. I am using react-query to make the fetch and also to cache the data. However, even though the function is wrapped in React.memo, useQuery causes multiple re-renders for no reason. It should be 2 renderings not 8.

import React, { memo } from "react";
import logo from "./logo.svg";
import "./App.css";
import { useQuery } from "react-query";
import Axios from "axios";

const fetchTodoList = async (key, id) => {
  const { data } = await Axios.get(
    `https://jsonplaceholder.typicode.com/posts/`
  );
  return data;
};

function App() {
  const { isLoading, isError, data, error } = useQuery("todos", fetchTodoList);
  console.log(isLoading, data);
  if (isLoading) {
    return <span>Loading...</span>;
  }

  if (isError) {
    return <span>Error: {error.message}</span>;
  }

  // also status === 'success', but "else" logic works, too
  return (
    <ul>
      {data.map((todo) => (
        <li key={todo.id}>{todo.title}</li>
      ))}
    </ul>
  );
}

export default memo(App);
0 Answers
Related