rooks
Version:
Collection of awesome react hooks
42 lines (41 loc) • 1.34 kB
TypeScript
/**
* Options for the useBroadcastChannel hook
*/
type UseBroadcastChannelOptions<T = any> = {
/**
* Callback function called when a message is received
*/
onMessage?: (data: T) => void;
/**
* Callback function called when an error occurs
*/
onError?: (error: Event) => void;
};
/**
* Return type for the useBroadcastChannel hook
*/
type UseBroadcastChannelReturn<T = any> = {
/**
* Function to send a message to the broadcast channel
*/
postMessage: (data: T) => void;
/**
* Function to manually close the broadcast channel
*/
close: () => void;
/**
* Whether the BroadcastChannel API is supported
*/
isSupported: boolean;
};
/**
* useBroadcastChannel
*
* @description A React hook that provides a clean interface to the Broadcast Channel API for cross-tab/window communication
* @param channelName - The name of the broadcast channel
* @param options - Configuration options for message and error handling
* @returns Object with postMessage function, close function, and isSupported flag
* @see {@link https://rooks.vercel.app/docs/hooks/useBroadcastChannel}
*/
declare function useBroadcastChannel<T = any>(channelName: string, options?: UseBroadcastChannelOptions<T>): UseBroadcastChannelReturn<T>;
export { useBroadcastChannel };