improve navigation effects

This commit is contained in:
SrGooglo 2024-09-15 03:33:28 +00:00
parent 7995ba452d
commit 6e30d97248

View File

@ -165,31 +165,46 @@ const NavigationController = (props) => {
state = {} state = {}
} }
app.location.last = window.location
await navigate(to, {
state
})
app.eventBus.emit("router.navigate", to, { app.eventBus.emit("router.navigate", to, {
state, state,
}) })
app.location.last = window.location return {
to,
return navigate(to, { state,
state }
})
} }
async function backLocation() { async function backLocation() {
app.eventBus.emit("router.navigate")
app.location.last = window.location
return window.history.back() return window.history.back()
} }
async function onHistoryChange() {
setTimeout(() => {
app.eventBus.emit("router.navigate", window.location.pathname, {
state: window.location.state,
})
}, 0)
}
React.useEffect(() => { React.useEffect(() => {
app.location = { app.location = {
last: window.location, last: window.location,
push: setLocation, push: setLocation,
back: backLocation, back: backLocation,
} }
window.addEventListener("popstate", onHistoryChange)
return () => {
window.removeEventListener("popstate", onHistoryChange)
}
}, []) }, [])
return props.children return props.children