mirror of
https://github.com/supabase/supabase.git
synced 2026-05-06 17:00:27 -04:00
ec86bbc4fe
Co-authored-by: Kang Ming <kang.ming1996@gmail.com> Co-authored-by: Joel Lee <lee.yi.jie.joel@gmail.com>
30 lines
714 B
TypeScript
30 lines
714 B
TypeScript
import { useState, useCallback, useEffect } from 'react'
|
|
|
|
const useHash = () => {
|
|
const [hash, setHash] = useState(() =>
|
|
typeof window !== 'undefined' ? window.location.hash.split('#')[1] : undefined
|
|
)
|
|
|
|
const hashChangeHandler = useCallback(() => {
|
|
setHash(window.location.hash.split('#')[1])
|
|
}, [])
|
|
|
|
useEffect(() => {
|
|
window.addEventListener('hashchange', hashChangeHandler)
|
|
return () => {
|
|
window.removeEventListener('hashchange', hashChangeHandler)
|
|
}
|
|
}, [hashChangeHandler])
|
|
|
|
const updateHash = useCallback(
|
|
(newHash) => {
|
|
if (newHash !== hash) window.location.hash = newHash
|
|
},
|
|
[hash]
|
|
)
|
|
|
|
return [hash, updateHash] as const
|
|
}
|
|
|
|
export default useHash
|