Skip to content

Instantly share code, notes, and snippets.

@drublic
Created September 22, 2019 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drublic/e7736ac2773708e0190c7722d0012c88 to your computer and use it in GitHub Desktop.
Save drublic/e7736ac2773708e0190c7722d0012c88 to your computer and use it in GitHub Desktop.
Custom Hook
// Testable Custom Hooks
import { useEffect } from "react";
// Function that can be tested with clear API, easily unit-testable
const handleHasLoader = ({ isLoading, hasFocus, setHasLoader }) => {
if (isLoading && !hasFocus) {
return setHasLoader(true);
}
return setHasLoader(false);
};
// Custom Hook implementing effect
const useHasLoader = ({ isLoading, hasFocus, setHasLoader }) => {
useEffect(
() =>
handleHasLoader({ isLoading, hasFocus, setHasLoader }),
[isLoading, hasFocus, setHasLoader],
);
};
export default useHasLoader;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment