【问题标题】:React + Firebase function looping by accidentReact + Firebase 函数意外循环
【发布时间】:2020-05-05 01:05:32
【问题描述】:

我不知道为什么当我使用 useStates 时我的函数会循环, 任何人都可以找出问题所在。 It loops over and over,this is what appears in my console.log inside the snapshot

`function Classtab() {
  const [userName, setuserName] = React.useState(null)
  const [userType, setuserType] = React.useState(null)
  const [userEmail, setuserEmail] = React.useState(null)
  const [userCourse, setuserCourse] = React.useState([])
  const [registeredCourse, setregisteredCourse] = React.useState([])
  firebase.auth().onAuthStateChanged((user) => {
    if(user){
        var db = firebase.firestore()
        db.collection('user').doc(user.uid)
            .get()
            .then(snapshot => {
                          setuserName( snapshot.data().name)
                          setuserType( snapshot.data().type)
                          setuserCourse( snapshot.data().course)
                          setuserEmail( user.email)
                          console.log(userCourse)
                userCourse.map(course => {
                  db.doc(course).get().then(
                        snapshot => {setregisteredCourse([...registeredCourse, snapshot.data().name])}
                        )
                      }
                    )
      }).catch(error => console.log(error))}else{}
    })
  return(...)`

【问题讨论】:

    标签: javascript reactjs firebase use-state


    【解决方案1】:

    您需要将您的授权代码移动到 useEffect 中。现在发生的事情是您在每次渲染时都在运行 onAuthStateChanged。并且每次返回时,都会导致另一个渲染,导致它无限添加更多订阅。

    我已修改您的代码以防止无限重新渲染并允许 userCourse 成为 promise.then 函数中的正确值。它原本应该让函数中的 userCourse 始终是一个空数组(由于闭包)。

    function Classtab() {
      const [userName, setuserName] = React.useState(null);
      const [userType, setuserType] = React.useState(null);
      const [userEmail, setuserEmail] = React.useState(null);
      const [userCourse, setuserCourse] = React.useState([]);
      const [registeredCourse, setregisteredCourse] = React.useState([]);
      const registeredCourseRef = useRef(registeredCourse);
      useEffect(()=>{
        registeredCourseRef.current = registeredCourse;
      },[registeredCourse])
      useEffect(() => {
        const unsubscribe = firebase.auth().onAuthStateChanged((user) => {
          if (user) {
            var db = firebase.firestore();
            db.collection('user')
              .doc(user.uid)
              .get()
              .then((snapshot) => {
                setuserName(snapshot.data().name);
                setuserType(snapshot.data().type);
                const userCourse = snapshot.data().course;
                setuserCourse(userCourse);
                setuserEmail(user.email);
                console.log(userCourse);
                userCourse.map((course) => {
                  db.doc(course)
                    .get()
                    .then((snapshot) => {
                      setregisteredCourse((registeredCourse)=>[
                        ...registeredCourse,
                        snapshot.data().name,
                      ]);
                    });
                });
              })
              .catch((error) => console.log(error));
          } else {
          }
        });
        return () => {
          unsubscribe();
        };
      //Need to have registeredCourse in the dependency array
      //Or have it in a ref
      }, []);
      // return(...)
    }
    

    【讨论】:

    • 它现在不渲染任何东西
    • 我把括号的位置弄乱了,希望现在效果更好
    • 我得到了大部分,但这里有一个问题,如果我在括号中没有registeredCourse,它不会推送状态。如果我把它放在括号里,它会再次开始循环
    • 我已经通过设置状态回调函数修复了它。
    猜你喜欢
    • 1970-01-01
    • 2013-04-03
    • 1970-01-01
    • 2020-08-13
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    相关资源
    最近更新 更多