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

Firebase Auth Signout 没有推送到 React 网络应用程序中的主页

Firebase Auth Signout 没有推送到 React 网络应用程序中的主页

海绵宝宝撒 2023-06-15 09:39:47
我有一个“注销”按钮,在提交时显然会删除用户会话。单击按钮时令牌将被删除,然后我希望它在成功删除令牌后重定向到主页。但是,单击按钮时会出现以下错误:未处理的承诺拒绝:TypeError:undefined is not an object (evaluating 'this.props')我不明白为什么会这样,因为原则上代码类似于调用“this.props.history.push”的其他函数。这是我的组件:class ProfileBox extends Component {  constructor(props) {    super(props)  }  async signOut(e) {    e.preventDefault()    await firebase.auth().signOut().then(function() {      console.log("Successfully signed out.")    }).catch(function(error) {      console.log(error)      console.log("An error occurred")    });    this.props.history.push("/");  }  render() {    return (          <button className="profile-box-logout" type="submit" name="button" onClick={this.signOut}>Logout</button>    )  }}谁能指出我做错了什么?class ProfileBox extends Component {  constructor(props) {    super(props)  }  async signOut(e) {    e.preventDefault()    await firebase.auth().signOut().then(function() {      console.log("Successfully signed out.")    }).catch(function(error) {      console.log(error)      console.log("An error occurred")    });    this.props.history.push("/");  }  render() {    return (          <button className="profile-box-logout" type="submit" name="button" onClick={this.signOut}>Logout</button>    )  }}谁能指出我做错了什么?
查看完整描述

1 回答

?
守着一只汪

TA贡献1872条经验 获得超3个赞

该问题与 Firebase 无关。this在该函数内部为 null,这会引发错误。如果您将函数移动到渲染器中,那么它将起作用。尝试这样的事情:


render() {


    const signOut = async (e) => {

      e.preventDefault();


      await firebase.auth().signOut().then(function() {

        console.log("Successfully signed out.")


      }).catch(function(error) {

        console.log(error)

        console.log("An error occurred")

      });


      this.props.history.push("/");

    }

    

    return (

      <button

        className="profile-box-logout"

        type="submit"

        name="button"

        onClick={signOut}

      >

        Logout

      </button>

    );

  }

这将使您可以访问this.props. 

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

添加回答

举报

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