Skip to content

Commit

Permalink
feat: start react query
Browse files Browse the repository at this point in the history
  • Loading branch information
laoriy committed Apr 14, 2024
1 parent 3e96a40 commit 8fe3c67
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
5 changes: 4 additions & 1 deletion 5.react/7.react-query/src/App.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import Todo from "./Todo";

function App() {
return (
<div className="App">
<header className="App-header">nihao</header>
<header className="App-header">to do list</header>
<hr />
<Todo></Todo>
</div>
);
}
Expand Down
32 changes: 32 additions & 0 deletions 5.react/7.react-query/src/Todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useQuery } from "@tanstack/react-query";
import axios from "axios";
async function fetchTodos() {
try {
return axios.get("/todos");
} catch (error) {
throw new Error(error);
}
}
function Todo() {
const { isError, isLoading, error, data } = useQuery({
queryKey: ["todos"],
queryFn: fetchTodos,
});

if (isLoading) return <p>Loading...</p>;
if (isError) return <p>{error.message}</p>;

return (
<section className="main">
<ul className="todo-list">
{data.map((todo) => (
<li key={todo.id} className={todo.completed ? "completed" : ""}>
{todo.title}
</li>
))}
</ul>
</section>
);
}

export default Todo;

0 comments on commit 8fe3c67

Please sign in to comment.