简体   繁体   English

登录 在导航栏中注销

[英]Login Logout in Navbar

I hope you are ok.我希望你还好。

I'm trying to switch to log out in Navabar when the user is logged in, but when I console log I get isloggedin false, even though the user is logged in and the token is stored in localstorage.我试图在用户登录时切换到在 Navabar 中注销,但是当我控制台日志时,我得到 isloggedin false,即使用户已登录并且令牌存储在 localstorage 中。

here is my code:这是我的代码:

App.js应用程序.js

 const App = () => { const [isLoggedIn, setLoggedIn] = useState(false) console.log(isLoggedIn) const handleLogin = token => { if (!token) return localStorage.setItem('token', token) setLoggedIn(true) } const handleLogout = () => () => { setLoggedIn(false) localStorage.clear() } return ( <div className="App"> <Router> <Navbar isLoggedIn={isLoggedIn} logout={handleLogout} /> <Switch> <Route exact path='/' component={props => ( <Login {...props} onLogin={handleLogin} /> )}></Route> <Route exact path="/signin" component={Signin}></Route> <Route exact path="/feed" component={Feed}></Route> <Route exact path="/withuser" component={Withuser}></Route> </Switch> </Router> </div> ) } export default App;
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

Navbar.js:导航栏.js:

 const TheNavbar = (props) => { console.log(props) return ( <div className="navbar"> <Navbar> <NavbarBrand> <NavLink id="logo" tag={Link} exact to="/"> <img src="/img/logounax.png" alt="image" style={{ width: 150 }} /> </NavLink> </NavbarBrand> <Nav> {props.isLoggedIn ? ( <Nav> <NavLink className="link-pink" tag={Link} exact to="/feed"> Profile </NavLink> <NavLink className="link-pink" tag={Link} exact to="/" onClick={props.logout()}> Logout </NavLink> </Nav> ) : ( <Nav> <NavLink className="link-pink" tag={Link} exact to="/"> Login </NavLink> <NavLink className="link-pink" tag={Link} exact to="/signin"> Signin </NavLink> </Nav> )} </Nav> </Navbar> </div> ) }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

Any ideas why is this happening?任何想法为什么会发生这种情况?

Thank you in advance.先感谢您。

(Post edited) (帖子已编辑)

I check your code there are some issue.我检查你的代码有一些问题。 You are trying toke check on handleLogin which will only check when you click on handleLogin .您正在尝试检查handleLogin ,它只会在您单击handleLogin时进行检查。 What happen if user refresh the page.如果用户刷新页面会发生什么。 It will ideally redirect to dashboard page if token is available.如果令牌可用,它将理想地重定向到仪表板页面。 So you have to add useEffect which checks on component mount.所以你必须添加useEffect来检查组件安装。 Basically you need to add this in your App.js component.基本上你需要在你的App.js组件中添加它。 It will check when component is mounted first time only and if token is available it will set it它只会检查组件何时第一次安装,如果令牌可用,它将设置它

useEffect(() => {
    if (localStorage.getItem("token")) {
      setLoggedIn(true);
    }
  }, []);

Just add this snippet in your file.只需在您的文件中添加此代码段即可。 It automatically works if every other things are correct.如果其他所有事情都正确,它会自动工作。

You still dont added complete code ;-).您仍然没有添加完整的代码;-)。 But I dont think it will required.但我认为不需要。 This will solve your that problem这将解决您的问题

I modify your sandbox.我修改了你的沙箱。 If you want to test the functionality type test for both field.如果要test两个字段的功能类型test Here is thedemo这是演示

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

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