繁体   English   中英

currentUser 未定义 显示未定义

[英]currentUser is undefined is displayed undefined

我对 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

Somhow 我通过添加这个来解决问题。

 const {user: currentUser} = useSelector((state) => state.auth);

感谢大家。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM