MCPcopy Create free account
hub / github.com/editablejs/editable / usePendingRoute

Function usePendingRoute

apps/docs/src/hooks/usePendingRoute.ts:4–35  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2import * as React from 'react'
3
4const usePendingRoute = () => {
5 const { events } = useRouter()
6 const [pendingRoute, setPendingRoute] = React.useState<string | null>(null)
7 const currentRoute = React.useRef<string | null>(null)
8 React.useEffect(() => {
9 let routeTransitionTimer: any = null
10
11 const handleRouteChangeStart = (url: string) => {
12 clearTimeout(routeTransitionTimer)
13 routeTransitionTimer = setTimeout(() => {
14 if (currentRoute.current !== url) {
15 currentRoute.current = url
16 setPendingRoute(url)
17 }
18 }, 100)
19 }
20 const handleRouteChangeComplete = () => {
21 setPendingRoute(null)
22 clearTimeout(routeTransitionTimer)
23 }
24 events.on('routeChangeStart', handleRouteChangeStart)
25 events.on('routeChangeComplete', handleRouteChangeComplete)
26
27 return () => {
28 events.off('routeChangeStart', handleRouteChangeStart)
29 events.off('routeChangeComplete', handleRouteChangeComplete)
30 clearTimeout(routeTransitionTimer)
31 }
32 }, [events])
33
34 return pendingRoute
35}
36
37export default usePendingRoute

Callers 2

CollapseSidebarFunction · 0.85
SidebarRouteTreeFunction · 0.85

Calls 2

onMethod · 0.80
offMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…