Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. import { useEffect } from "react";
  2. import { useError } from "./use-error";
  3.  
  4. /**
  5. * React useEffect hook to cover componentDidmount
  6. *
  7. * @example
  8. * useOnMount()
  9. */
  10. export function useOnMount(args: IuseOneMount) {
  11. const { throwError } = useError();
  12.  
  13. useEffect(() => {
  14. for (let arg of args) {
  15. if (typeof arg.handler === "function") {
  16. try {
  17. arg.handler(...arg.params);
  18. } catch (error) {
  19. throwError(error);
  20. }
  21. } else {
  22. (console
  23. ? console.warn || console.log
  24. : (m: any) => {
  25. return m;
  26. })(`useOnMount: All handlers should be functions`);
  27. }
  28. }
  29. }, []);
  30. }
  31.  
  32. type IuseOneMount = Array<{
  33. handler: Array<(...args: any) => void>;
  34. params: Array<any>;
  35. }>;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement