Open History to see the last commit
1import * as React from 'react'
2import {
3 getLocationChangeInfo,
4 handleHashScroll,
5 trimPathRight,
6} from '@tanstack/router-core'
7import { useLayoutEffect, usePrevious } from './utils'
8import { useRouter } from './useRouter'
9import { useRouterState } from './useRouterState'
10β
11export function Transitioner() {
12 const router = useRouter()
13 const mountLoadForRouter = React.useRef({ router, mounted: false })
14β
15 const [isTransitioning, setIsTransitioning] = React.useState(false)
16 // Track pending state changes
17 const { hasPendingMatches, isLoading } = useRouterState({
18 select: (s) => ({
19 isLoading: s.isLoading,
20 hasPendingMatches: s.matches.some((d) => d.status === 'pending'),
21 }),
22 structuralSharing: true,
23 })
24β
25 const previousIsLoading = usePrevious(isLoading)
26β
27 const isAnyPending = isLoading || isTransitioning || hasPendingMatches
28 const previousIsAnyPending = usePrevious(isAnyPending)
29β
30 const isPagePending = isLoading || hasPendingMatches
31 const previousIsPagePending = usePrevious(isPagePending)
32β
33 router.startTransition = (fn: () => void) => {
34 setIsTransitioning(true)
35 React.startTransition(() => {
36 fn()
37 setIsTransitioning(false)
38 })
39 }
40β
41 // Subscribe to location changes
42 // and try to load the new location
43 React.useEffect(() => {
44 const unsub = router.history.subscribe(router.load)
45β
46 const nextLocation = router.buildLocation({
47 to: router.latestLocation.pathname,
48 search: true,
49 params: true,
50 hash: true,
51 state: true,
52 _includeValidateSearch: true,
53 })
54β
55 if (
56 trimPathRight(router.latestLocation.href) !==
57 trimPathRight(nextLocation.href)
58 ) {
59 router.commitLocation({ ...nextLocation, replace: true })
60 }
61β
62 return () => {
63 unsub()
64 }
65 }, [router, router.history])
66β
67 // Try to load the initial location
68 useLayoutEffect(() => {
69 if (
70 // if we are hydrating from SSR, loading is triggered in ssr-client
71 (typeof window !== 'undefined' && router.ssr) ||
72 (mountLoadForRouter.current.router === router &&
73 mountLoadForRouter.current.mounted)
74 ) {
75 return
76 }
77 mountLoadForRouter.current = { router, mounted: true }
78β
79 const tryLoad = async () => {
80 try {
81 await router.load()
82 } catch (err) {
83 console.error(err)
84 }
85 }
86β
87 tryLoad()
88 }, [router])
89β
90 useLayoutEffect(() => {
91 // The router was loading and now it's not
92 if (previousIsLoading && !isLoading) {
93 router.emit({
94 type: 'onLoad', // When the new URL has committed, when the new matches have been loaded into state.matches
95 ...getLocationChangeInfo(router.state),
96 })
97 }
98 }, [previousIsLoading, router, isLoading])
99β
100 useLayoutEffect(() => {
101 // emit onBeforeRouteMount
102 if (previousIsPagePending && !isPagePending) {
103 router.emit({
104 type: 'onBeforeRouteMount',
105 ...getLocationChangeInfo(router.state),
106 })
107 }
108 }, [isPagePending, previousIsPagePending, router])
109β
110 useLayoutEffect(() => {
111 // The router was pending and now it's not
112 if (previousIsAnyPending && !isAnyPending) {
113 router.emit({
114 type: 'onResolved',
115 ...getLocationChangeInfo(router.state),
116 })
117β
118 router.__store.setState((s) => ({
119 ...s,
120 status: 'idle',
121 resolvedLocation: s.location,
122 }))
123β
124 handleHashScroll(router)
125 }
126 }, [isAnyPending, previousIsAnyPending, router])
127β
128 return null
129}