Last active
July 14, 2023 16:40
-
-
Save willie-hung/7578ddc503b81b2aaf2cac320d3e1c29 to your computer and use it in GitHub Desktop.
post_9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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