@progress/sitefinity-nextjs-sdk
Version:
Provides OOB widgets developed using the Next.js framework, which includes an abstraction layer for Sitefinity communication. Additionally, it offers an expanded API, typings, and tools for further development and integration.
32 lines (31 loc) • 1.08 kB
JavaScript
'use client';
import { useCallback, useEffect, useState } from 'react';
export const EVENTS = {
PERSONALIZED_WIDGETS_LOADED: 'widgetPersnalizationLoaded'
};
/**
* Returns a stateful event payload value and a function to dispatch an event with data.
* @param eventKey The custom event key to be dispatched and listened to.
* @param attach Whether to create an event listener in the current hook instance or skip it.
*/
export function useSfEvents(eventKey, attach = true) {
const [payload, setState] = useState(null);
const setPayload = useCallback((data) => {
const event = new CustomEvent(eventKey, {
detail: data
});
document.dispatchEvent(event);
}, [eventKey]);
useEffect(() => {
if (attach) {
const ln = (e) => {
setState(e.detail);
};
document.addEventListener(eventKey, ln);
return () => {
document.removeEventListener(eventKey, ln);
};
}
}, [attach, eventKey]);
return [payload, setPayload];
}