use-broadcast-ts
Version:
Use the Broadcast Channel API in React easily with hooks or Zustand, and Typescript!
25 lines (24 loc) • 861 B
TypeScript
/**
* Our hook will return an object with three properties:
* - send: a function that will send a message to all other tabs
* - state: the current state of the broadcast
* - subscribe: a function that will subscribe to the broadcast (Only if options.subscribe is true)
*/
export type UseBroadcastReturn<T> = {
send: (val: T) => void;
state: T | undefined;
subscribe: (callback: (e: T) => void) => () => void;
};
/**
* The options for the useBroadcast hook
*/
export type UseBroadcastOptions = {
subscribe?: boolean;
};
/**
*
* @param name The name of the broadcast channel
* @param val The initial value of the broadcast
* @returns Returns an object with three properties: send, state and subscribe
*/
export declare const useBroadcast: <T>(name: string, val?: T | undefined, options?: UseBroadcastOptions) => UseBroadcastReturn<T>;