Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 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. * handler: (a,b) => a + b
  10. * params: [{ a: 1, b: 2 }]
  11. * }])
  12. */
  13. export function useOnMount(args: IuseOneMount) {
  14. const { throwError } = useError();
  15.  
  16. useEffect(() => {
  17. for (let arg of args) {
  18. if (typeof arg.handler === "function") {
  19. try {
  20. arg.handler(...arg.params);
  21. } catch (error) {
  22. throwError(error);
  23. }
  24. } else {
  25. (console
  26. ? console.warn || console.log
  27. : (m: any) => {
  28. return m;
  29. })(`useOnMount: All handlers should be functions`);
  30. }
  31. }
  32. }, []);
  33. }
  34.  
  35. type IuseOneMount = Array<{
  36. handler: (...args: any) => void;
  37. params: Array<any>;
  38. }>;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement