@tanstack/react-router 0.0.1-beta.236 → 0.0.1-beta.238

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tanstack/react-router",
3
3
  "author": "Tanner Linsley",
4
- "version": "0.0.1-beta.236",
4
+ "version": "0.0.1-beta.238",
5
5
  "license": "MIT",
6
6
  "repository": "tanstack/router",
7
7
  "homepage": "https://tanstack.com/router",
@@ -44,7 +44,7 @@
44
44
  "@tanstack/store": "^0.1.3",
45
45
  "tiny-invariant": "^1.3.1",
46
46
  "tiny-warning": "^1.0.3",
47
- "@tanstack/history": "0.0.1-beta.236"
47
+ "@tanstack/history": "0.0.1-beta.238"
48
48
  },
49
49
  "scripts": {
50
50
  "build": "rollup --config rollup.config.js"
package/src/Matches.tsx CHANGED
@@ -47,13 +47,13 @@ export interface RouteMatch<
47
47
  export type AnyRouteMatch = RouteMatch<any>
48
48
 
49
49
  export function Matches() {
50
- const { routesById } = useRouter()
50
+ const router = useRouter()
51
51
  const routerState = useRouterState()
52
52
  const matches = routerState.pendingMatches?.some((d) => d.showPending)
53
53
  ? routerState.pendingMatches
54
54
  : routerState.matches
55
- const locationKey = useRouterState().location.state.key
56
- const route = routesById[rootRouteId]!
55
+ const locationKey = router.latestLocation.state.key
56
+ const route = router.routesById[rootRouteId]!
57
57
 
58
58
  const errorComponent = React.useCallback(
59
59
  (props: any) => {
@@ -19,7 +19,7 @@ import {
19
19
  RouterOptions,
20
20
  RouterState,
21
21
  } from './router'
22
- import { NoInfer, PickAsRequired, useLayoutEffect } from './utils'
22
+ import { NoInfer, PickAsRequired, pick, useLayoutEffect } from './utils'
23
23
  import { MatchRouteOptions } from './Matches'
24
24
  import { RouteMatch } from './Matches'
25
25
 
@@ -87,7 +87,12 @@ export function RouterProvider<
87
87
  },
88
88
  } as any)
89
89
 
90
- const inner = <RouterProviderInner<TRouteTree, TDehydrated> router={router} />
90
+ const inner = (
91
+ <routerContext.Provider value={router}>
92
+ <Matches />
93
+ <Transitioner />
94
+ </routerContext.Provider>
95
+ )
91
96
 
92
97
  if (router.options.Wrap) {
93
98
  return <router.options.Wrap>{inner}</router.options.Wrap>
@@ -96,10 +101,13 @@ export function RouterProvider<
96
101
  return inner
97
102
  }
98
103
 
99
- function RouterProviderInner<
100
- TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
101
- TDehydrated extends Record<string, any> = Record<string, any>,
102
- >({ router }: RouterProps<TRouteTree, TDehydrated>) {
104
+ function Transitioner() {
105
+ const router = useRouter()
106
+ const routerState = useRouterState({
107
+ select: (s) =>
108
+ pick(s, ['isLoading', 'location', 'resolvedLocation', 'isTransitioning']),
109
+ })
110
+
103
111
  const [isTransitioning, startReactTransition] = React.useTransition()
104
112
 
105
113
  router.startReactTransition = startReactTransition
@@ -114,19 +122,27 @@ function RouterProviderInner<
114
122
  }, [isTransitioning])
115
123
 
116
124
  const tryLoad = () => {
117
- // startReactTransition(() => {
118
- try {
119
- router.load()
120
- } catch (err) {
121
- console.error(err)
125
+ const apply = (cb: () => void) => {
126
+ if (!routerState.isTransitioning) {
127
+ startReactTransition(() => cb())
128
+ } else {
129
+ cb()
130
+ }
122
131
  }
123
- // })
132
+
133
+ apply(() => {
134
+ try {
135
+ router.load()
136
+ } catch (err) {
137
+ console.error(err)
138
+ }
139
+ })
124
140
  }
125
141
 
126
142
  useLayoutEffect(() => {
127
143
  const unsub = router.history.subscribe(() => {
128
144
  router.latestLocation = router.parseLocation(router.latestLocation)
129
- if (router.state.location !== router.latestLocation) {
145
+ if (routerState.location !== router.latestLocation) {
130
146
  tryLoad()
131
147
  }
132
148
  })
@@ -138,7 +154,7 @@ function RouterProviderInner<
138
154
  state: true,
139
155
  })
140
156
 
141
- if (router.state.location.href !== nextLocation.href) {
157
+ if (routerState.location.href !== nextLocation.href) {
142
158
  router.commitLocation({ ...nextLocation, replace: true })
143
159
  }
144
160
 
@@ -150,14 +166,16 @@ function RouterProviderInner<
150
166
  useLayoutEffect(() => {
151
167
  if (
152
168
  !isTransitioning &&
153
- router.state.resolvedLocation !== router.state.location
169
+ !routerState.isLoading &&
170
+ routerState.resolvedLocation !== routerState.location
154
171
  ) {
172
+ console.log('onResolved', routerState.location)
155
173
  router.emit({
156
174
  type: 'onResolved',
157
- fromLocation: router.state.resolvedLocation,
158
- toLocation: router.state.location,
175
+ fromLocation: routerState.resolvedLocation,
176
+ toLocation: routerState.location,
159
177
  pathChanged:
160
- router.state.location!.href !== router.state.resolvedLocation?.href,
178
+ routerState.location!.href !== routerState.resolvedLocation?.href,
161
179
  })
162
180
  router.pendingMatches = []
163
181
 
@@ -167,7 +185,7 @@ function RouterProviderInner<
167
185
  resolvedLocation: s.location,
168
186
  }))
169
187
  }
170
- }, [isTransitioning])
188
+ }, [isTransitioning, routerState.isLoading])
171
189
 
172
190
  useLayoutEffect(() => {
173
191
  if (!window.__TSR_DEHYDRATED__) {
@@ -175,11 +193,7 @@ function RouterProviderInner<
175
193
  }
176
194
  }, [])
177
195
 
178
- return (
179
- <routerContext.Provider value={router}>
180
- <Matches />
181
- </routerContext.Provider>
182
- )
196
+ return null
183
197
  }
184
198
 
185
199
  export function getRouteMatch<TRouteTree extends AnyRoute>(