Created
February 4, 2019 15:08
-
-
Save tannerlinsley/a28df3aa2f79b8d2ba25c499b53675c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const useLogout = () => { | |
// Use any store you want! | |
const [_, setStore] = Store.useStore() | |
// Have a localStorage hook? | |
const [_, setUserToken] = useLocalStorage('userToken') | |
// Have a router hook? | |
const { navigate } = useLocation() | |
return () => { | |
// Easy! | |
setStore(draft => { | |
draft.auth.loading = true | |
draft.auth.jwt = null | |
draft.auth.user = null | |
}) | |
setUserToken(undefined) | |
navigate('/') | |
} | |
} | |
const App = () => { | |
const logout = useLogout() | |
return <button onClick={logout}>Logout</button> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment