Upgrade to React 18 GitOrigin-RevId: 9b81936e6eea2bccd97fe5c2c5841f0b946371b8
16 lines
401 B
TypeScript
16 lines
401 B
TypeScript
import { useEffect, useRef } from 'react'
|
|
|
|
export const useUserChannel = (): BroadcastChannel | null => {
|
|
const channelRef = useRef<BroadcastChannel | null>(null)
|
|
|
|
if (channelRef.current === null && 'BroadcastChannel' in window) {
|
|
channelRef.current = new BroadcastChannel('user')
|
|
}
|
|
|
|
useEffect(() => {
|
|
return () => channelRef.current?.close?.()
|
|
}, [])
|
|
|
|
return channelRef.current
|
|
}
|