currentUser未定义,显示未定义

Sidou Didous

我使用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;

格雷格·科努什(Greg Konush)

重定向到您的Profile组件的组件具有在用户登录时进行重定向的逻辑,这很可能是您通过某些useEffect挂钩完成的。另外,该重定向组件需要正确填充您的Redux存储,因此当您重定向到该Profile组件时,该组件将currentUser在存储中可用,否则它将始终呈现Redirect

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章