mirror of
https://github.com/ragestudio/comty.git
synced 2025-07-08 16:54:15 +00:00
Sync active key with URL on browser navigation
This commit is contained in:
parent
0d94ecc46d
commit
218df1c284
@ -1,10 +1,9 @@
|
|||||||
import React from "react"
|
import React from "react"
|
||||||
|
|
||||||
export default ({
|
export default ({ defaultKey = "0", queryKey = "key" }) => {
|
||||||
defaultKey = "0",
|
const [activeKey, setActiveKey] = React.useState(
|
||||||
queryKey = "key",
|
new URLSearchParams(window.location.search).get(queryKey) ?? defaultKey,
|
||||||
}) => {
|
)
|
||||||
const [activeKey, setActiveKey] = React.useState(new URLSearchParams(window.location.search).get(queryKey) ?? defaultKey)
|
|
||||||
|
|
||||||
const replaceQueryTypeToCurrentTab = (key) => {
|
const replaceQueryTypeToCurrentTab = (key) => {
|
||||||
if (!key) {
|
if (!key) {
|
||||||
@ -20,8 +19,21 @@ export default ({
|
|||||||
replaceQueryTypeToCurrentTab(key)
|
replaceQueryTypeToCurrentTab(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
const onHistoryChange = () => {
|
||||||
activeKey,
|
const newActiveKey = new URLSearchParams(window.location.search).get(
|
||||||
changeActiveKey,
|
queryKey,
|
||||||
]
|
)
|
||||||
|
|
||||||
|
setActiveKey(newActiveKey ?? defaultKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
window.addEventListener("popstate", onHistoryChange)
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener("popstate", onHistoryChange)
|
||||||
|
}
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
return [activeKey, changeActiveKey]
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user