reactjs - 反应路由器重定向 - 无法读取未定义的属性“道具”
问题描述
谁能帮我理解这里出了什么问题?
我试图在注销后重定向到登录页面。
这是代码
import React, { Component } from "react";
import firebase from "../Firebase/firebase";
import { withRouter, Redirect } from "react-router-dom";
class Home extends Component {
constructor(props) {
super(props);
this.logout = this.logout.bind(this);
}
state = {};
logout = e => {
e.preventDefault();
firebase
.auth()
.signOut()
.then(
function() {
console.log("Signed Out");
this.props.history.push("/login");
},
function(error) {
console.error("Sign Out Error", error);
}
);
};
render() {
return (
<div align="center" className="container">
<h1> Home page </h1>
<br />
<br />
<h2>Hello: {this.state.user} </h2>
<button
className="btn btn-lg text-white"
style={{ backgroundColor: "#B65DF3" }}
onClick={this.logout}
>
Logout
</button>
</div>
);
}
}
export default withRouter(Home);
这是错误消息
TypeError:无法读取未定义的属性“道具”
我不明白为什么它不起作用,因为我在此处重定向的登录页面完全相同并且有效。
解决方案
您的问题将是您传递的匿名函数.then
。尝试用粗箭头或类似的东西替换它:
logout = (e) => {
e.preventDefault();
firebase.auth().signOut().then(this.handleResolve).catch(this.handleError)
}
handleResolve = () => {
console.log('Signed Out');
this.props.history.push('/login')
}
handleError = (error) => {
console.error('Sign Out Error', error);
}
推荐阅读
- iis - W3 服务停止,但 w3wp.exe 仍在运行数秒
- java - 尝试从 Selenium Web 自动化的下拉列表中选择一个选项 -error-“ElementNotInteractableException:无法滚动到视图中”
- docker - 为什么 Docker 会阻止将容器附加到主机和用户定义的桥接网络?
- sql - 如何在将批次插入表时提交循环内的每次迭代
- javascript - 在 HTML/CSS 中给画布同样的最大高度和宽度
- javascript - JavaScript 构造函数中的 this = {} 表达式是什么意思?
- vue.js - v-list 中的 v-list 不适用于 vuetify
- java - 使用 Java 使用 Apache POI 删除 Word 中的域代码
- firebase - 在 Flutter 中查询 Firestore 数据库数组中存在的数据
- regex - 列名正则表达式 - Pandas