Skip to content

Commit 39179f1

Browse files
committed
feat: update posts list without page refresh using React Query
1 parent 7d00ad0 commit 39179f1

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

app/src/components/PostForm.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import type { ReactElement } from "react";
22
import { useState } from "react";
3+
import { useQueryClient } from "@tanstack/react-query";
34

45
import createPost from "../api/createPost";
56

67
export default function PostForm(): ReactElement {
8+
const queryClient = useQueryClient();
79
const [form, setForm] = useState({
810
head: "",
911
body: "",
@@ -15,7 +17,8 @@ export default function PostForm(): ReactElement {
1517
}
1618
createPost(form)
1719
.then(() => {
18-
window.location.reload();
20+
setForm({ head: "", body: "" }); // Reset form
21+
queryClient.invalidateQueries({ queryKey: ["posts"] }); // Trigger posts refetch
1922
})
2023
.catch((error) => {
2124
console.error(error); // eslint-disable-line no-console

0 commit comments

Comments
 (0)