useCrossTab

The useCrossTab hook provides a synchronized state across multiple tabs (or iframes) of the same origin using the BroadcastChannel API. It works similarly to useSignal, but any changes to the state are broadcasted to all open tabs, ensuring consistent state updates across different browser contexts.

Demo

Open second demo page to see state being synchronize across tabs

count: 0

Usage

const count = useCrossTab("counter", 0)
count.value += 1

Type Declaration

import { const useCrossTab: <T>(name: string, initialValue: T) => Signal<T>useCrossTab } from "@kaioken-core/hooks"

const const count: Signal<number>count = useCrossTab<number>(name: string, initialValue: number): Signal<number>useCrossTab("counter", 0)

const count: Signal<number>count.Signal<number>.value: numbervalue += 1