mirror of
https://github.com/NeonGamerBot-QK/saahild.com.git
synced 2025-05-14 17:33:05 +00:00
Make Hashrouter auto switch inline
This commit is contained in:
parent
87c4f48258
commit
372f16b010
3 changed files with 13 additions and 30 deletions
|
@ -1,12 +1,22 @@
|
|||
// writing my own hashRouter
|
||||
|
||||
import { useEffect, useState } from "react"
|
||||
|
||||
export function HashRouter({ children }: any) {
|
||||
const [hash, setHash] = useState(window.location.hash)
|
||||
if(process.env.NODE_ENV !== "production") console.log(children)
|
||||
if(!Array.isArray(children)) children = [children]
|
||||
debugger;
|
||||
useEffect(() => {
|
||||
const ev = () => {
|
||||
setHash(window.location.hash)
|
||||
}
|
||||
window.addEventListener('hashchange', ev)
|
||||
return () => window.removeEventListener('hashchange', ev)
|
||||
})
|
||||
// debugger;
|
||||
const child = children.find((child:any) => {
|
||||
if(process.env.NODE_ENV !== "production") console.log('script', child.props, window.location.hash, child.props.route == window.location.hash, (window.location.hash.length < 2 && child.props.index))
|
||||
return child.props.path == window.location.hash || (window.location.hash.length < 2 && child.props.index) || child.props.path == "*"
|
||||
if(process.env.NODE_ENV !== "production") console.log('script', child.props, window.location.hash, child.props.route == hash, (hash.length < 2 && child.props.index))
|
||||
return child.props.path == hash || (hash.length < 2 && child.props.index) || child.props.path == "*"
|
||||
})
|
||||
// console.log('child', child)
|
||||
if(child) return child.props.component;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue