英文:
React - useEffect with dependency still fires on first render
问题
你的组件为什么在触发第二个Effect钩子时不等待点击按钮事件?
BlogPost.jsx
import React from "react";
import { useNavigate, useParams } from "react-router-dom";
import { useState, useEffect } from "react";
import { Footer } from "../components/Footer";
export const BlogPost = () => {
const [blogData, setBlogData] = useState({title: null, content: null, tags: null}); // 初始化状态,以便在useEffect获取数据之前显示 "null"。
const [deletePost, setDeletePost] = useState(false); // 初始化标志 "现在是时候删除帖子了"。
const navigate = useNavigate();
const urlParams = useParams();
/* 第一个钩子在挂载后立即触发,检索博客文章的数据 */
useEffect(() => {
const retrieveBlog = async () => {
// 我们需要使用博客标题向restdb.io发出查询,以检索内容和作者。
const response = await fetch(`https://reactblogapp-52d2.restdb.io/rest/excel-blog?q={"title":"${urlParams.id}"}`,
{
"headers": {
'Cache-Control': 'no-cache',
'x-apikey': '63d53b073bc6b255ed0c43c2',
'content-type': 'application/json'
}
}
);
if (!response.ok) {
console.log("数据库调用响应不正常!");
navigate("/notfound");
} else {
const res = await response.json();
setBlogData(res[0]);
}
}
retrieveBlog().catch((error) => (console.log("出现错误:" + error)));
}, []);
/* 第二个effect钩子应在用户点击删除按钮时触发,但实际上在第一个钩子之后立即触发。 */
useEffect(() => {
const str = `https://reactblogapp-52d2.restdb.io/rest/excel-blog/${blogData._id}"`;
console.log(str);
console.log(blogData);
const deleteFunction = async () => {
const response = await fetch(str,
{
"method": 'DELETE',
"headers":
{
'Cache-Control': 'no-cache',
'x-apikey': '63d53b073bc6b255ed0c43c2',
'content-type': 'application/json'
}
});
if (!response.ok) {
console.log("数据库调用响应不正常!现在无法删除帖子。");
navigate("/notfound"); // 钩子在用户点击按钮之前触发,blogData设置为undefined,因此它总是重定向到notfound,因为获取失败。
} else {
const res = await response.json();
if (res["result"] === 0)
alert("现在无法删除此帖子!抱歉造成不便。");
}
}
deleteFunction().catch((error) => (console.log("出现错误:" + error)));
}, [deletePost]);
return (
<div>
{!!blogData.title ? <h1>{blogData.title}</h1> : null}
{!!blogData.content ? <p>{blogData.content}</p> : null}
{!!blogData.tags ? <h5>{blogData.tags}</h5> : null}
<button onClick={() => setDeletePost(true)}>删除此帖子</button>
<Footer />
</div>
);
}
你的代码中有两个useEffect钩子,第一个在组件挂载后触发,第二个在deletePost
状态变化时触发。问题可能是由于deletePost
状态的更改导致第二个useEffect早于预期触发。你可以确保只有在点击"删除此帖子"按钮时才将deletePost
状态设置为true
,这样第二个useEffect只会在用户点击按钮时触发。如果它仍然在第一个useEffect之前触发,请确保没有其他地方更改了deletePost
状态。
英文:
I'm having a weird issue in my component. I'm developing a blog app, and this specific component should display the selected blog post. In order to do that, on render it should fetch the data from a web database and display it. Then the component should provide a button that lets the user delete the specific post.
To achieve the above, I thought of using two useEffect hooks: the first triggers right after the mounting phase, ( empty dependencies []), the second triggers when the user clicks the button "Delete this post". The button has a onClick() event handler that switches the state variable deletePost to true; since the second useEffect hook has that state variable in its dependencies, it should trigger and send the fetch DELETE to the web db.
What happens, instead, is that the component gets mounted, the first useEffect fires but suddenly the second hook fires aswell, while blogData is set to undefined; the fetch DELETE fails and the user gets redirected to the error page.
I thought that it was a problem in the onClick event handler in the button definition, but I believe it's coded correctly.
BlogPost.jsx
import React from "react";
import { useNavigate, useParams } from "react-router-dom";
import { useState, useEffect } from "react";
import { Footer } from "../components/Footer";
export const BlogPost = () =>
{
const [blogData, setBlogData] = useState({title : null, content : null, tags : null}); // Initialization of state, in order to display "null" until useEffect fetches the data.
const [deletePost, setDeletePost] = useState(false); // Initialization of the flag "it's time to delete the post"
const navigate = useNavigate();
const urlParams = useParams();
/* first hook fires right after mounting and it retrieves the blog post's data */
useEffect(() => {
const retrieveBlog = async () =>
{
// We need to make a query to restdb.io with the blog title to retrieve content and author.
const response = await fetch("https://reactblogapp-52d2.restdb.io/rest/excel-blog?q={\"title\":\"" + urlParams.id + "\"}",
{
"headers" : {
'Cache-Control' : "no-cache",
'x-apikey': '63d53b073bc6b255ed0c43c2',
'content-type': 'application/json'
}
}
);
if (!response.ok)
{
console.log("Database call response not ok!");
navigate("/notfound");
}
else
{
const res = await response.json();
setBlogData(res[0]);
}
}
retrieveBlog().catch((error) => (console.log("There was an error: " + error)));
}, []);
/* Second effect hook should fire when the user clicks on the delete button, it fires instead right after the first hook. */
useEffect(() => {
const str = "https://reactblogapp-52d2.restdb.io/rest/excel-blog/" + blogData._id + "\"}";
console.log(str);
console.log(blogData);
const deleteFunction = async () =>
{
const response = await fetch(str,
{
"method" : 'DELETE',
"headers" :
{
'Cache-Control' : "no-cache",
'x-apikey': '63d53b073bc6b255ed0c43c2',
'content-type': 'application/json'
}
});
if (!response.ok)
{
console.log("Database call response not ok! Can't delete the post now.");
navigate("/notfound"); // hook fires BEFORE the user clicks on the button, blogData is set to undefined and it always redirects it to notfound, because the fetch fails.
}
else
{
const res = await response.json();
if (res["result"] === 0)
alert("Can't delete this post now! Sorry for the inconvenient.");
}
}
deleteFunction().catch((error) => (console.log("There was an error: " + error)));
}, [deletePost]);
return(
<div>
{!!blogData.title ? <h1>{blogData.title}</h1> : null}
{!!blogData.content ? <p>{blogData.content}</p> : null}
{!!blogData.tags ? <h5>{blogData.tags}</h5> : null}
<button onClick={() => setDeletePost(true)}>Delete this post</button>
<Footer />
</div>
);
}
Why is my component not waiting for the onClick button event when firing the second Effect hook ?
答案1
得分: 1
> 但突然第二个钩子也被触发,同时blogData被设置为undefined;
你可以在第二个useEffect
内部使用简单的条件来阻止函数被调用,例如:
if (!deletePost) return;
或者
if (!blogData) return;
但这明显是一种变通方法。
> 我认为每次网络请求/ API 调用在组件的生命周期中被视为“副作用”,因此它应该放在 useEffect 钩子中。
API调用应该在useEffect
钩子内部调用,但只有在你想在组件的特定状态(生命周期)下调用API时才需要,例如,如果你想在组件挂载时获取一些数据,你需要将它放在useEffect
中。但是,删除调用不应该在组件状态改变时被调用。它应该在用户操作(事件)上被调用,即onClick
。
基本上,只需将删除调用应用到按钮上。
const handleDeletion = async () => {
const response = await fetch(str, ...);
};
<button onClick={handleDeletion}>删除这篇文章</button>
英文:
> but suddenly the second hook fires aswell, while blogData is set to undefined;
You could e.g. prevent the function from being called with a simple condition inside the second useEffect
, like:
if (!deletePost) return;
or
if (!blogData) return;
but this is definitely a workaround.
> I thought that every web request / API call is considered a "side effect" in regards of the component's lifecycle, and thus it should be put in a useEffect hook
API calls should be called within useEffect
hook, but only if you want to call the API in specified state (lifecycle) of the component, e.g. if you want to fetch some data on component mount, you need to place it in the useEffect
. But the deletion call is not supposed to be called when the component state chages. It's supposed to be called on user-action (event), that is onClick
.
Basically, just apply the deletion call to the button.
const handleDeletion = async () => {
const response = await fetch(str, ...);
};
<button onClick={handleDeletion}>Delete this post</button>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论