成功登录后立即注销

问题描述 投票:1回答:1

我使用reactjsrest服务开发了一个应用程序。现在我想为用户添加登录和注销选项。我已创建成功创建了登录页面,当我单击登录按钮时,它会重定向到主页面并立即注销。

我也清理了本地存储。

logout() {
localStorage.clear();
window.location.href = './login';
}

我能知道哪里出错了吗?

      <Button href="#" onClick={this.logout()}>LOGOUT</Button>
reactjs rest session login logout
1个回答
1
投票

发生这种情况是因为您使用了函数调用而不是传递函数引用。

将处理程序传递给子组件时,您必须传递引用而不是函数调用

更改

<Button href="#" onClick={this.logout()}>LOGOUT</Button>

<Button href="#" onClick={this.logout}>LOGOUT</Button>
© www.soinside.com 2019 - 2024. All rights reserved.