@excentone/spfx-react
Version:
Contains custom ReactJs components and hooks intended to use when developing SharePoint Framework (SPFx) Web components.
17 lines (15 loc) • 716 B
JavaScript
import { useRef, useCallback, useLayoutEffect } from "react";
/**
* `useSafeDispatch` wraps the specified {@link dispatch} method to safely dispatch while the React component is mounted.
* @param dispatch The dispatch method to invoke if the React component is still mounted.
* @returns A function that conditionally executes the dispatch method.
*/
export const useSafeDispatch = (dispatch) => {
const mounted = useRef(false);
useLayoutEffect(() => {
mounted.current = true;
return () => { mounted.current = false; };
}, []);
return useCallback((...args) => (mounted.current && dispatch ? dispatch(...args) : void 0), [dispatch]);
};
//# sourceMappingURL=useSafeDispatch.js.map