Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs React Hook useEffect缺少依赖项:';fetchComments';。包括它或删除依赖项数组_Reactjs_React Hooks - Fatal编程技术网

Reactjs React Hook useEffect缺少依赖项:';fetchComments';。包括它或删除依赖项数组

Reactjs React Hook useEffect缺少依赖项:';fetchComments';。包括它或删除依赖项数组,reactjs,react-hooks,Reactjs,React Hooks,为什么它缺少依赖性问题 const [ comments, setComment ] = useState([]) const fetchComments = async () => { const res = await axios.get(`http://localhost:4001/posts/${postid}/comments`); setComment(res.data); } useEffect(() => {

为什么它缺少依赖性问题

const [ comments, setComment ] = useState([])

    const fetchComments = async () => {
        const res = await axios.get(`http://localhost:4001/posts/${postid}/comments`);
        setComment(res.data);
    }

    useEffect(() => {
        fetchComments();
    }, [postid]);

    console.log("IsArray", Array.isArray(comments)); // Returns me true, true, true then warning after warning it become false, false
为什么它会这样做?我对post列表使用了相同的方法。它确实工作得很好,但使用特定的ID获取它会给我发送错误。请注意

“postid”不是useEffect挂钩的直接依赖项。因此,要么将fetchComments函数移到useEffect钩子中(如下所示)

或者添加fetchComments函数作为依赖项

useEffect(() => {
   fetchComments();
}, [fetchComments]);

这是埃斯林的错误。因为
useffect
在其回调函数中使用
fetchComments
,所以有一些方法可以解决它

  • 通过useCallback包装
    fetchComments
    ,然后将
    fetchComments
    包含在依赖项数组中
const fetchComments=useCallback(异步()=>{
const res=等待axios.get(`http://localhost:4001/posts/${postid}/comments`);
setComment(res.data);
},[posted])
useffect(()=>{
fetchComments();

},[fetchComments])提供给useEffect的参数意味着useEffect中的函数将在参数更改时运行。所以,useffect依赖于这个参数

在这里,您提供了postid,作为useEffect的依赖项,但没有在useEffect中使用它。这就是为什么react建议postid在这里没有用处,或者将fetchComment作为依赖项包含,或者从那里删除postid

因为,只要更改了postid,您就需要运行fetchcomments, 你可以像这样简单地重写它

const [ comments, setComment ] = useState([])

const fetchComments = async (postid) => {
    const res = await axios.get(`http://localhost:4001/posts/${postid}/comments`);
    setComment(res.data);
}

useEffect(() => {
    fetchComments(postid);
}, [postid]);

在第二个选项中,如果不使用useCallback包装
fetchComments
,则可能会递归地重新渲染。查看我的注释以了解更多详细信息。如果我尝试const renderComments=comments.map(comment=>{return(
  • {comment.content}
  • )})console.log(renderComments)它确实说comments.map不是一个函数,但我做console.log(comments它确实显示数据)@SunilDubey数据的类型是什么?你能把它包括在你的问题里吗?确保它是一个数组。是的,它是66b2077b:array(13)0:{id:“aa1aba56”,content:“trister”}1:{id:“d8efbcd1”,content:“test”}@SunilDubey只有一个地方可以调用
    setComment(res.data)
    如果
    res.data
    是一个数组,则不会出现该问题和`console.log(“IsArray”,array.IsArray(comments));`一定是真的。有了您发布的信息,如果
    res.data
    是数组,则
    comments
    不可能成为非数组类型。哦,我得到了它,感谢您的支持,我返回的是对象而不是数组
    const [ comments, setComment ] = useState([])
    
    const fetchComments = async (postid) => {
        const res = await axios.get(`http://localhost:4001/posts/${postid}/comments`);
        setComment(res.data);
    }
    
    useEffect(() => {
        fetchComments(postid);
    }, [postid]);