@anton.bobrov/react-vevet-hooks
Version:
A collection of custom React hooks designed to seamlessly integrate with the `Vevet` library
35 lines (34 loc) • 1.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useOnPageLoad = void 0;
var react_1 = require("react");
var vevet_1 = require("vevet");
/**
* Custom React hook that triggers an effect once the page has fully loaded.
*
* @example
* const MyComponent = () => {
* useOnPageLoad(() => {
* console.log('Page has loaded!');
* }, []);
*
* return <div>Welcome to my website!</div>;
* };
*/
function useOnPageLoad(effect, deps) {
(0, react_1.useEffect)(function () {
var unsubscribe;
var promise = vevet_1.vevet.onPageLoad();
promise
.then(function () {
unsubscribe = effect();
})
.catch(function () { });
return function () {
promise.cancel();
unsubscribe === null || unsubscribe === void 0 ? void 0 : unsubscribe();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, deps);
}
exports.useOnPageLoad = useOnPageLoad;