Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { useEffect } from "react";
- import { useError } from "./use-error";
- /**
- * React useEffect hook to cover componentDidmount
- *
- * @example
- * useOnMount()
- */
- export function useOnMount(args: IuseOneMount) {
- const { throwError } = useError();
- useEffect(() => {
- for (let arg of args) {
- if (typeof arg.handler === "function") {
- try {
- arg.handler(...arg.params);
- } catch (error) {
- throwError(error);
- }
- } else {
- (console
- ? console.warn || console.log
- : (m: any) => {
- return m;
- })(`useOnMount: All handlers should be functions`);
- }
- }
- }, []);
- }
- type IuseOneMount = Array<{
- handler: Array<(...args: any) => void>;
- params: Array<any>;
- }>;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement