使用 React History.push() 重定向不会反映输入

我正在使用 Firebase 构建一个应用程序并做出反应。

在该应用程序中,当用户编辑其信息并按下提交按钮时,它会通过history.push()将其重定向到用户信息页面。

用户信息和页面重定向效果都很好,但是页面重定向后,编辑过的用户信息没有体现出来,页面显示的是之前版本的用户信息。

这是我的代码。

我使用 AuthContext 将 Firebase 凭据传递给每个组件。这是 AuthContext 文件的一部分。


  function updateUser(username, email, data) {

    const uid = data.uid

    db.collection('users').doc(uid).set({

      email: email,

      username: username,

    }, {merge: true})

  }


  useEffect(() => {

    const unsubscribe = auth.onAuthStateChanged(async(user) => {

      if (user) {

        const uid = user.uid

        console.log(uid)

        await db.collection('users').doc(uid).get()

          .then(snapshot => {

            const data = snapshot.data()

            setCurrentUser(data)

            setLoading(false)

          })

      }

    })

    

    return unsubscribe

  }, [])

您知道如何反映我在编辑屏幕上输入的值吗?


GCT1015
浏览 143回答 1
1回答

婷婷同学_

因为updateUser是一个异步函数,所以您应该在函数中使用async awaitor.then语法updateUser,以便它history.push在更新用户数据后调用function updateUser(username, email, data) {    const uid = data.uid    return db.collection('users').doc(uid).set({        email: email,        username: username,      }, {merge: true})  }     updateUser(usernameRef.current.value, emailRef.current.value, data)       .then(() => {           history.push('/dashboard');       }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript