Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logout util - consider making a logout button component instead #16

Open
rooberrydev opened this issue Oct 26, 2020 · 1 comment
Open

Comments

@rooberrydev
Copy link

rooberrydev commented Oct 26, 2020

You might want to combine the logout util and the logout button. The reason I say this is

  1. because you'll probably want to redirect back to the / route when the user logs out, and this requires the useHistory() hook, which can only be called in a component function body or a custom hook
  2. because you're unlikely to call the logout function outside the logout button.

The recommended way to redirect explicitly in react router 5 is the useHistory() hook, combined with history.push()

import {useHistory} from "react-router-dom"
const LogoutButton = () => {
  const history = useHistory()
  
  const logout = auth()
    .signOut()
    .then(() => {
      history.push('/');
    })
    .catch((error) => {
      console.error(error);
    });

  return (<button onClick={logout}>Log Out</button>)
}

then render this logout button on the sidebar👍

@rooberrydev
Copy link
Author

an alternative is #20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant