我有一个调用 API 的简单应用程序。我正在使用 react-query 进行提取并缓存数据。但是,即使该函数被包装在 中React.memo
,也会useQuery
无缘无故地导致多次重新渲染。它应该是 2 个渲染而不是 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);