当用户尝试重定向到我从 props.location 发送的私有(private)路由时,以便在登录后用户重定向回同一链接。
但是当用户直接登录页面时,它会显示
私有(private)路线
<Redirect to={{ pathname: '/sign-in', state: { from: props.location} }} />
登录页面:
componentWillReceiveProps(nextProps) {
if (nextProps.apiData !== this.props.apiData && nextProps.apiData !== false) {
if(!(this.props.location.state.from.pathname)) {
this.props.history.push(this.props.location.state.from.pathname)
}
else{
if (this.props.location.length === 0) {
this.props.history.push("/");
}
else{
this.props.history.goBack();
}
}
}
}
它给了我这个错误
类型错误:无法读取未定义的“来自”属性
最佳答案
this.props.location.state
仅当组件通过 Redirect
挂载时才存在,因此在访问它之前需要先检查它是否存在:
if (this.props.location.state && !this.props.location.state.from.pathname) {
关于ReactJS:类型错误:无法读取未定义的属性 'from',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50169632/