UseEffect 无限循环

huangapple go评论94阅读模式
英文:

UseEffect endless loop

问题

I'm new to React and I'm trying to learn why I have an endless loop with the following code. I'm using firebase to retrieve a single document.

import services from '../utilities/services';
import { useState, useEffect } from 'react';
import { useParams } from 'react-router-dom';

function SchoolDetail() {
    const { schoolID } = useParams();
    const [school, setSchool] = useState();
    const [id, setID] = useState(schoolID);

    useEffect(() => {
        services.getSchool(schoolID).then((response) => {
            setSchool(response);
        }).catch((e) => {
            alert("Error occurred while fetching the school. " + e);
        }, [id])

    })

    return (
        <>
        {(school) && console.log(school.id)}
        Details
        </>
    );
}

export default SchoolDetail;

I've tried using [] as the useEffect dependency and still get an endless loop. I've tried [schoolID], and I've tried the state variable [id]. All cause an endless loop. What am I missing? Is it something to do with the getSchool function returning a promise?

英文:

I'm new to React and I'm trying to learn why I have an endless loop with the following code. I'm using firebase to retrieve a single document.

import services from &#39;../utilities/services&#39;;
import {useState, useEffect} from &#39;react&#39;;
import {useParams} from &#39;react-router-dom&#39;;

function SchoolDetail(){
    const {schoolID} = useParams();
    const [school, setSchool] = useState();
    const [id, setID] = useState(schoolID);
    


    useEffect(()=&gt;{

        services.getSchool(schoolID).then((response) =&gt; {
            setSchool(response);
        }).catch((e) =&gt; {
            alert(&quot;Error occured while fetching the school. &quot; + e);
        },[id])

    })

    return(
    &lt;&gt;
    {(school) &amp;&amp; console.log(school.id)}
    Details
    &lt;/&gt;
    );
}

export default SchoolDetail;.

I've tried using [] as the useEffect dependency and still get an endless loop. I've tried [schoolID], and I've tried the state variable [id]. All cause an endless loop. What am I missing? Is is something to do with the getSchool function returning a promise?

答案1

得分: 1

问题是由于没有将依赖数组作为useEffect的第二个参数传递,并且在useEffect回调内部设置了id状态,这会触发useEffect的重复调用。
为了确保useEffect仅在挂载时运行一次,您应该将一个空数组作为依赖数组传递:

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

此外,您可以删除id状态并直接使用schoolID作为useEffect的依赖项:

const { schoolID } = useParams();
const [school, setSchool] = useState();

useEffect(() => {
  ...
}, [schoolID]);
英文:

The problem of an endless loop is caused by not passing a dependency array as the second argument of useEffect, and by setting the id state inside the useEffect callback, which triggers the useEffect to be called repeatedly.
To ensure that useEffect only runs once, on mount, you should pass an empty array as the dependency array:

useEffect(() =&gt; {
  ...
}, []);

Moreover you can remove the id state and directly use schoolID as the dependency of useEffect:

const { schoolID } = useParams();
const [school, setSchool] = useState();

useEffect(() =&gt; {
  ...
}, [schoolId]);

huangapple
  • 本文由 发表于 2023年3月10日 00:40:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687560.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定