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

utils to hooks - @azizi #20

Open
rooberrydev opened this issue Oct 27, 2020 · 0 comments
Open

utils to hooks - @azizi #20

rooberrydev opened this issue Oct 27, 2020 · 0 comments

Comments

@rooberrydev
Copy link

rooberrydev commented Oct 27, 2020

I haven't done much custom hook writing but as far as I know the main difference between a regular function and a custom hook is that a custom hook lets you call other hooks inside it.

also to use a custom hook you have to call it at the top level in a component, so you would need to return something from the custom hook.

This would actually be handy for the logout function, converting it to a hook allows us to call the useHistory() directly, and return a function that uses history.push().

import { auth } from '../connection.js';
import { useHistory } from 'react-router-dom';
const useLogout = () => {
  const history = useHistory();
  return () => {
    auth()
      .signOut()
      .then(() => {
        history.push('/');
      })
      .catch((error) => {
        console.error(error);
      });
  };
};

export default useLogout;

to use this, you would have to call useLogout inside Sidebar

const Sidebar = () => {
const logout = useLogout();
....
....
return <button onClick={logout} />
}

This is an alternative to what i suggested in #16

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