我使用redux进行反应,并且遇到这个问题,导致登录名和个人资料之间出现无限循环。当我在配置文件中删除条件if(!cuurentUser)进行调试时,它说curentUser未定义。
那请帮我
先感谢您。
import React from "react";
import { Redirect } from 'react-router-dom';
import { useSelector } from "react-redux";
const Profile = () => {
const { currentUser }= useSelector((state) => state.auth);
if (!currentUser) {
return <Redirect to="/login" />;
}
return (
<div className="container">
<header className="jumbotron">
<h3>
<strong>{currentUser.username}</strong> Profile
</h3>
</header>
<p>
<strong>Token:</strong> {currentUser.accessToken.substring(0, 20)} ...{" "}
{currentUser.accessToken.substr(currentUser.accessToken.length - 20)}
</p>
<p>
<strong>Id:</strong> {currentUser.id}
</p>
<p>
<strong>Email:</strong> {currentUser.email}
</p>
<strong>Authorities:</strong>
<ul>
{currentUser.roles &&
currentUser.roles.map((role, index) => <li key={index}>{role}</li>)}
</ul>
</div>
);
};
export default Profile;
重定向到您的Profile
组件的组件具有在用户登录时进行重定向的逻辑,这很可能是您通过某些useEffect
挂钩完成的。另外,该重定向组件需要正确填充您的Redux存储,因此当您重定向到该Profile
组件时,该组件将currentUser
在存储中可用,否则它将始终呈现Redirect
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句