Skip to content

useSynchronizedState

The useSynchronizedState hook allows you to synchronize async or sync states across different tabs or windows in a web application using the Broadcast Channel API. This is useful for sharing state between multiple instances of your application.

Add the utility

Terminal window
npx @ivnatsr/ezreact add use-synchronized-state

Parameters

  • key: A string that serves as the key for the Broadcast Channel.
  • initialState (optional): The initial state value or a function that returns the initial state.
  • track (optional): A state value that, when changed, will trigger a broadcast to other tabs or windows. Useful for tracking asynchronous states. If provided, you don’t need to manually broadcast updates through the channel, since the hook will perform this action automatically.

Return

This hook returns an object containing:

  • state: The current synchronized state.
  • broadcast: A function that can be called to manually broadcast a new state value.

Example

import { useRef } from 'react'
import { useSynchronizedState } from './path/to/use-synchronized-state'
const SynchronizedStateExample = () => {
const count = useRef(0)
const { state, broadcast } = useSynchronizedState({
key: 'example-channel',
initialState: count.current
})
const increment = () => {
broadcast(++count.current)
}
return (
<div>
<h1>Current Count: {state}</h1>
<button onClick={increment}>Increment</button>
</div>
)
}
export default SynchronizedStateExample