DEV Community

Discussion on: How to mimic componentDidUpdate() with React Hooks

 
devdufutur profile image
Rudy Nappée • Edited

I guess your RequireLogon component could have been a custom hook...

function useRequireLogon(isLoggedIn, expiration, loadPermissions) {
  const refExpiration = useRef(expiration);
  useEffect(() => {
    if (isLoggedIn && expiration !== refExpiration.current) {
      loadPermissions();
    }
    refExpiration.current = expiration;
  }, [expiration, isLoggedIn, refExpiration]);
}
Thread Thread
 
shiraze profile image
shiraze

I like that, primarily because what I had wasn't a "component" as such, but some sort of rule. This is certainly cleaner code. Thanks!