为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

GCT1015 2023-07-14 16:40:51
我正在使用 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  }, [])您知道如何反映我在编辑屏幕上输入的值吗?
查看完整描述

1 回答

?
婷婷同学_

TA贡献1844条经验 获得超8个赞

因为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');

       }


查看完整回答
反对 回复 2023-07-14
  • 1 回答
  • 0 关注
  • 135 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信