我将react与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;
发布于 2020-11-28 06:54:55
重定向到您的Profile组件的组件有一个在用户登录时进行重定向的逻辑,很可能您正在使用某个useEffect钩子进行重定向。此外,该重定向组件需要正确填充您的Redux存储,以便当您重定向到Profile组件时,您的currentUser在存储中可用,否则它将始终呈现Redirect
发布于 2020-11-29 07:21:30
我是通过添加这个来解决这个问题的。
const {user: currentUser} = useSelector((state) => state.auth);
谢谢大家。
https://stackoverflow.com/questions/65044571
复制相似问题