Scusf0
Scusf0

Reputation: 333

How to logout in react

I'm trying to logout using this method: In render I have a div like this:

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

and the logout function:

logout() {
    // localStorage.clear();
    location.href = 'localhost:3000';
}

At localhost:3000 I have the login page.

Put when I press logout, nothing happends. What can I do? Thank you

Upvotes: 12

Views: 46198

Answers (2)

Isaacs Katongole
Isaacs Katongole

Reputation: 341

this solved my problem

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

Upvotes: 23

Yuri Pereira
Yuri Pereira

Reputation: 1995

Give to us the Minimal Example to be tested.

You already checked if your function is being called? Try to put some console.log inside logout(), it smells Binding to methods of React class issue.

Try to insert http:// inside location.href, like this: location.href = 'http://localhost:3000';

Upvotes: 3

Related Questions