Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 248
Scusf0 Avatar asked Aug 07 '17 17:08

Scusf0


People also ask

How do I log into my React account?

Login page in ReactJS designFirstly, we will make a form - that will have fields of email and password. After this, there will be a button to submit the information filled in these fields. Basically, it is the information that is sent to the backend for validation.

How do I logout of react native?

If you'd like to sign the user out of their current authentication state, call the signOut method: import auth from '@react-native-firebase/auth'; auth() . signOut() .


2 Answers

this solved my problem

 logout() {
        localStorage.clear();
        window.location.href = '/';
    }
like image 79
Isaacs Katongole Avatar answered Nov 02 '22 04:11

Isaacs Katongole


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';

like image 33
Yuri Ramos Avatar answered Nov 02 '22 04:11

Yuri Ramos