Reactjs React Router TypeError:无法读取属性';州';未定义的

Reactjs React Router TypeError:无法读取属性';州';未定义的,reactjs,redux,react-router,Reactjs,Redux,React Router,我在react和redux中制作了一个基本的路由应用程序,当我想重定向到推荐人时,我遇到了一个错误。 行中发生错误: this.referer=props.location.state.referer | |“/”; 这是我的登录组件 import React,{Component,createRef}来自“React”; 从“react redux”导入{connect}; 从“react router dom”导入{Redirect}; 从“axios”导入axios; 从“/redux

我在react和redux中制作了一个基本的路由应用程序,当我想重定向到推荐人时,我遇到了一个错误。 行中发生错误:


this.referer=props.location.state.referer | |“/”;
这是我的登录组件

import React,{Component,createRef}来自“React”;
从“react redux”导入{connect};
从“react router dom”导入{Redirect};
从“axios”导入axios;
从“/redux/actions/actions”导入{updateAuthTokens,updateLoggedin}”;
类登录扩展组件{
建造师(道具){
超级(道具);
this.idRef=createRef();
this.pwdRef=createRef();
此.state={
伊斯洛格丁:错,
伊瑟罗:错,
标识符:“”,
密码:“”,
};
this.postLogin=this.postLogin.bind(this);
this.referer=props.location.state.referer | |“/”;
}
登录后(e){
e、 预防默认值();
常量标识符=this.idRef.current.value;
const password=this.pwdRef.current.value;
axios
.post(process.env.REACT\u APP\u BASE\u URL+“/auth/local”{
标识符,
密码,
})
。然后((结果)=>{
如果(result.status==200){
this.props.ul(true);
这个.props.uat(result.jwt);
this.setState({isLoggedIn:true});
}否则{
this.setState({isError:true});
}
})
.catch((e)=>{
this.setState({isError:true});
});
}
render(){
if(this.state.isLoggedIn){
返回;
}否则{
返回(
登录
{this.state.isError&&(
提供的用户名或密码不正确

)} ); } } } 常量mapStateToProps=(状态)=>{ 返回{ isLoggedIn:state.isLoggedIn, }; }; const mapDispatchToProps={ ul:updateLoggedin, uat:updateAuthTokens, }; 导出默认连接(mapStateToProps、mapDispatchToProps)(登录);
从该组件执行重定向:

从“React”导入React;
从“react redux”导入{connect};
从“react router dom”导入{Route,Redirect};
类PrivateRoute扩展了React.Component{
render(){
const{isLoggedIn,children,…rest}=this.props;
如果(isLoggedIn){
返回{children};
}否则{
返回(
);
}
}
}
常量mapStateToProps=(状态)=>{
返回{
isLoggedIn:state.isLoggedIn,
};
};
导出默认连接(MapStateTops,null)(PrivateRoute);
我的路线是:

import React,{Component}来自“React”;
从“react Router dom”导入{BrowserRouter as Router,Switch,Route,Link};
从“react redux”导入{connect};
从“/Home”导入主页;
从“/Admin”导入管理员;
从“/Members”导入成员;
从“/Login”导入登录名;
从“/PrivateRoute”导入PrivateRoute;
导入“/App.css”;
类应用程序扩展组件{
render(){
返回(
  • 主页
  • 成员
  • 管理
); } } 常量mapStateToProps=(状态)=>{ 返回{ isLoggedIn:state.isLoggedIn, }; }; 导出默认连接(MapStateTops,null)(应用程序);
我一直无法让它工作,不得不对它进行评论,这意味着我总是在主页上,而不是我预定的位置


如果您有任何帮助/见解,我们将不胜感激。

当道具发生变化时,请访问位置:



您不会总是重定向,因此请更改:

this.referer=props.location.state.referer | |“/”;

this.referer=props.location.state?props.location.state.referer:“/”;
太棒了!成功了:)谢谢。