useEffect的第二个参数是一个依赖数组。当一个或多个依赖项从上次渲染发生更改时,将调用效果。您收到此错误是因为您没有在依赖项数组中包含所有必要的变量或函数,这可能导致数据过时和意外行为。
例如,您的钩子PostBody.jsx
缺少依赖项params.id
。解决方法是简单地将值添加到依赖数组中:
// PostBody.jsx
useEffect(() =>
{
// Your code here
}, [params.id, ...yourOtherDependencies]) // Add params.id to your dependency array
对丢失的钩子getAction
和allComments
.
关于filter
错误,您没有在第一个条件下返回值:
const newCommentArr = CommentLists.filter((comment, index) =>
{
if (newComment._id === comment._id)
{
indexValue = index
// You need to return a value!
}
else
{
return newComment._id !== comment._id
}
})
您返回的值取决于您使用它的目的。您可能只需将代码更改为:
const newCommentArr = CommentLists.filter((comment) => newComment._id === comment._id)
或者
const newCommentArr = CommentLists.filter((comment) => newComment._id !== comment._id)
根据您使用它的目的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句