Firebase auth onAuthChange proper way to use

I’m trying to use firebase auth module but when I try to make it work with it’s method to unsubscribe it just won’t call the method itself. My first aproach was without unsubscribe method, with worked but could make me have memory leaks:

useEffect(() => {
    firebase.auth().onAuthStateChanged((user) => {
      // My method
    });
  }, []);

Then I have been checking few websites and stackoverflow questions and they say it should be done like this:

useEffect(() => {
    const unsubscribe = firebase.auth().onAuthStateChanged((user) => {
      // My method
    });

    return unsubscribe();
  }, []);

This just does not call the firebase.auth().onAuthChanged method. Any suggestions?

Thanks very much.

11 thoughts on “Firebase auth onAuthChange proper way to use”

  1. Definitely believe that which you said. Your
    favorite reason seemed to be on the internet the easiest thing to be aware of.
    I say to you, I certainly get annoyed while people consider worries that they just don’t know about.

    You managed to hit the nail upon the top and also defined out the whole
    thing without having side effect , people can take a signal.
    Will likely be back to get more. Thanks

    Reply
  2. This is the right blog for anybody who would like to find
    out about this topic. You realize a whole lot its almost tough to argue with you (not that I actually would want to…HaHa).
    You definitely put a brand new spin on a
    subject that has been written about for a long time. Excellent
    stuff, just wonderful!

    Reply
  3. You’re so cool! I do not suppose I’ve truly read through a single thing like that before.
    So wonderful to find another person with a few original thoughts on this subject matter.
    Really.. thanks for starting this up. This web
    site is one thing that’s needed on the internet, someone with a
    bit of originality!

    Reply

Leave a Comment