我正在从我的快递后端获取并显示一系列帖子,它显示为“博客”组件。
该组件有一个从后端删除它然后重新获取帖子的按钮,每当我单击删除按钮时,状态会从“空闲”变为“加载”再到“成功”,然后在重新获取数据时将其删除。
这行得通,但是当它被删除时,下一篇文章会显示“成功”作为状态。为什么?
BlogPost.jsx:
import React from "react";
import axios from "axios";
import { useQueryCache, useMutation } from "react-query";
const deleteBlog = ({ postID }) => {
return axios.delete(`/api/posts/${postID}`, { withCredentials: true });
};
const BlogPost = ({ postID, title, content }) => {
const [mutation, { status }] = useMutation(deleteBlog);
const queryCache = useQueryCache();
return (
<div style={{ border: "medium solid red", padding: 5 }}>
{`status: ${status}`}
<button
onClick={() => {
mutation({ postID }).then(() => {
queryCache.invalidateQueries("posts");
});
}}
>
delete
</button>
<h2>{title}</h2>
<h3>{postID}</h3>
<p>{content}</p>
</div>
);
};
export default BlogPost;
登陆.jsx:
import React from "react";
import BlogPost from "../components/BlogPost";
import axios from "axios";
import { useQuery } from "react-query";
const posts = () => {
return axios.get("/api/posts").then((resp) => resp.data);
};
const Landing = () => {
const { data, isSuccess, refetch } = useQuery("posts", posts);
return (
<div>
<button
onClick={() =>
axios
.post(
"/api/posts",
{ title: "title", content: "content" },
{ withCredentials: true }
)
.then(() => refetch())
}
>
New post
</button>
{isSuccess &&
data.map((post, index) => {
return (
<BlogPost
key={index}
postID={post._id}
title={post.title}
createdDate={post.createdAt}
author={post.author}
content={post.content}
/>
);
})}
</div>
);
};
export default Landing;
快递后台:
let fakedb = [
{
_id: "0",
title: "title",
createdAt: "1234",
author: "Lorem",
content: "453",
},
];
app.get("/api/posts", (req, res) => {
res.json(fakedb);
});
app.post("/api/posts", (req, res) => {
const { title, content } = req.body;
fakedb.push({
title,
_id: fakedb.length.toString(),
content,
createdAt: "456",
author: "1",
});
res.sendStatus(200);
});
app.delete("/api/posts/:postID", (req, res) => {
fakedb = fakedb.filter((i) => i._id !== req.params.postID);
res.sendStatus(200);
});