Skip to content

Instantly share code, notes, and snippets.

@willie-hung
Last active July 14, 2023 16:40
Show Gist options
  • Save willie-hung/7578ddc503b81b2aaf2cac320d3e1c29 to your computer and use it in GitHub Desktop.
Save willie-hung/7578ddc503b81b2aaf2cac320d3e1c29 to your computer and use it in GitHub Desktop.
post_9
import { useQuery } from "@tanstack/react-query";
import axios from "axios";
interface Todo {
id: number;
title: string;
userId: number;
completed: boolean;
}
const TodoList = () => {
const fetchTodos = () =>
axios
.get<Todo[]>("https://jsonplaceholder.typicode.com/todos")
.then((res) => res.data);
const { data, error, isLoading } = useQuery<Todo[], Error>({
queryKey: ["todos"],
queryFn: fetchTodos,
});
if (error) return <p>{error.message}</p>;
if (isLoading) return <p>Loading...</p>;
return (
<ul className="list-group">
{data?.map((todo) => (
<li key={todo.id} className="list-group-item">
{todo.title}
</li>
))}
</ul>
);
};
export default TodoList;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment