Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Enhanced useHash.ts hook ^^ (#22374)

Einshine || Shine269c03e36a2e
diff --git a/apps/docs/hooks/useHash.ts b/apps/docs/hooks/useHash.ts
index 3c6f9567dab..29824c1742b 100644
--- a/apps/docs/hooks/useHash.ts
+++ b/apps/docs/hooks/useHash.ts
@@ -1,12 +1,17 @@
import { useState, useCallback, useEffect } from 'react'
const useHash = () => {
- const [hash, setHash] = useState(() =>
- typeof window !== 'undefined' ? window.location.hash.split('#')[1] : undefined
- )
+ const [hash, setHash] = useState(() => {
+ if (typeof window !== 'undefined') {
+ const url = new URL(window.location.href)
+ return url.hash.substring(1) // Removes the leading '#'
+ }
+ return undefined
+ })
const hashChangeHandler = useCallback(() => {
- setHash(window.location.hash.split('#')[1])
+ const url = new URL(window.location.href)
+ setHash(url.hash.substring(1))
}, [])
useEffect(() => {
@@ -18,7 +23,9 @@ const useHash = () => {
const updateHash = useCallback(
(newHash) => {
- if (newHash !== hash) window.location.hash = newHash
+ if (newHash !== hash) {
+ window.location.hash = newHash
+ }
},
[hash]
)