@react-navigation/routers 8.0.0-alpha.10 → 8.0.0-alpha.12

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/src/TabRouter.tsx CHANGED
@@ -91,10 +91,16 @@ const getRouteHistory = (
91
91
  backBehavior: BackBehavior,
92
92
  initialRouteName: string | undefined
93
93
  ) => {
94
+ const route = routes[index];
95
+
96
+ if (route == null) {
97
+ throw new Error(`Couldn't find a route at index ${index}.`);
98
+ }
99
+
94
100
  const history = [
95
101
  {
96
102
  type: TYPE_ROUTE,
97
- key: routes[index].key,
103
+ key: route.key,
98
104
  },
99
105
  ];
100
106
 
@@ -103,17 +109,29 @@ const getRouteHistory = (
103
109
  switch (backBehavior) {
104
110
  case 'order':
105
111
  for (let i = index; i > 0; i--) {
112
+ const route = routes[i - 1];
113
+
114
+ if (route == null) {
115
+ throw new Error(`Couldn't find a route at index ${i - 1}.`);
116
+ }
117
+
106
118
  history.unshift({
107
119
  type: TYPE_ROUTE,
108
- key: routes[i - 1].key,
120
+ key: route.key,
109
121
  });
110
122
  }
111
123
  break;
112
124
  case 'firstRoute':
113
125
  if (index !== 0) {
126
+ const route = routes[0];
127
+
128
+ if (route == null) {
129
+ throw new Error("Couldn't find a route at index 0.");
130
+ }
131
+
114
132
  history.unshift({
115
133
  type: TYPE_ROUTE,
116
- key: routes[0].key,
134
+ key: route.key,
117
135
  });
118
136
  }
119
137
  break;
@@ -124,9 +142,17 @@ const getRouteHistory = (
124
142
  initialRouteIndex = initialRouteIndex === -1 ? 0 : initialRouteIndex;
125
143
 
126
144
  if (index !== initialRouteIndex) {
145
+ const route = routes[initialRouteIndex];
146
+
147
+ if (route == null) {
148
+ throw new Error(
149
+ `Couldn't find a route at index ${initialRouteIndex}.`
150
+ );
151
+ }
152
+
127
153
  history.unshift({
128
154
  type: TYPE_ROUTE,
129
- key: routes[initialRouteIndex].key,
155
+ key: route.key,
130
156
  });
131
157
  }
132
158
  break;
@@ -150,6 +176,10 @@ const changeIndex = (
150
176
  if (backBehavior === 'history' || backBehavior === 'fullHistory') {
151
177
  const currentRoute = state.routes[index];
152
178
 
179
+ if (currentRoute == null) {
180
+ throw new Error(`Couldn't find a route at index ${index}.`);
181
+ }
182
+
153
183
  if (backBehavior === 'history') {
154
184
  // Remove the existing key from the history to de-duplicate it
155
185
  history = history.filter((it) =>
@@ -266,8 +296,9 @@ export function TabRouter({
266
296
  } as Route<string>;
267
297
  });
268
298
 
299
+ const currentRoute = state.routes[state.index ?? 0];
269
300
  const index = Math.min(
270
- Math.max(routeNames.indexOf(state.routes[state?.index ?? 0]?.name), 0),
301
+ Math.max(currentRoute ? routeNames.indexOf(currentRoute.name) : 0, 0),
271
302
  routes.length - 1
272
303
  );
273
304
 
@@ -311,10 +342,13 @@ export function TabRouter({
311
342
  }
312
343
  );
313
344
 
314
- const index = Math.max(
315
- 0,
316
- routeNames.indexOf(state.routes[state.index].name)
317
- );
345
+ const currentRoute = state.routes[state.index];
346
+
347
+ if (currentRoute == null) {
348
+ throw new Error(`Couldn't find a route at index ${state.index}.`);
349
+ }
350
+
351
+ const index = Math.max(0, routeNames.indexOf(currentRoute.name));
318
352
 
319
353
  let history = state.history.filter(
320
354
  // Type will always be 'route' for tabs, but could be different in a router extending this (e.g. drawer)
@@ -414,10 +448,18 @@ export function TabRouter({
414
448
  initialRouteName
415
449
  );
416
450
 
451
+ const updatedRoute = updatedState.routes[updatedState.index];
452
+
453
+ if (updatedRoute == null) {
454
+ throw new Error(
455
+ `Couldn't find a route at index ${updatedState.index}.`
456
+ );
457
+ }
458
+
417
459
  return {
418
460
  ...updatedState,
419
461
  preloadedRouteKeys: updatedState.preloadedRouteKeys.filter(
420
- (key) => key !== state.routes[updatedState.index].key
462
+ (key) => key !== updatedRoute.key
421
463
  ),
422
464
  };
423
465
  }
@@ -425,6 +467,10 @@ export function TabRouter({
425
467
  case 'GO_BACK': {
426
468
  const focusedRoute = state.routes[state.index];
427
469
 
470
+ if (focusedRoute == null) {
471
+ throw new Error(`Couldn't find a route at index ${state.index}.`);
472
+ }
473
+
428
474
  if (state.history.length === 1 && !focusedRoute.history?.length) {
429
475
  return null;
430
476
  }
@@ -453,7 +499,12 @@ export function TabRouter({
453
499
  }
454
500
 
455
501
  const previousHistoryItem = state.history[state.history.length - 2];
456
- const previousKey = previousHistoryItem?.key;
502
+
503
+ if (previousHistoryItem == null) {
504
+ return null;
505
+ }
506
+
507
+ const previousKey = previousHistoryItem.key;
457
508
  const index = state.routes.findLastIndex(
458
509
  (route) => route.key === previousKey
459
510
  );
@@ -464,13 +515,19 @@ export function TabRouter({
464
515
 
465
516
  let routes = state.routes;
466
517
 
518
+ const previousRoute = routes[index];
519
+
520
+ if (previousRoute == null) {
521
+ throw new Error(`Couldn't find a route at index ${index}.`);
522
+ }
523
+
467
524
  if (
468
525
  backBehavior === 'fullHistory' &&
469
- routes[index].params !== previousHistoryItem.params
526
+ previousRoute.params !== previousHistoryItem.params
470
527
  ) {
471
528
  routes = [...state.routes];
472
529
  routes[index] = {
473
- ...routes[index],
530
+ ...previousRoute,
474
531
  params: previousHistoryItem.params,
475
532
  };
476
533
  }
@@ -479,7 +536,7 @@ export function TabRouter({
479
536
  ...state,
480
537
  routes,
481
538
  preloadedRouteKeys: state.preloadedRouteKeys.filter(
482
- (key) => key !== state.routes[index].key
539
+ (key) => key !== previousRoute.key
483
540
  ),
484
541
  history: state.history.slice(0, -1),
485
542
  index,
@@ -497,6 +554,10 @@ export function TabRouter({
497
554
 
498
555
  const route = state.routes[routeIndex];
499
556
 
557
+ if (route == null) {
558
+ throw new Error(`Couldn't find a route at index ${routeIndex}.`);
559
+ }
560
+
500
561
  const getId = routeGetIdList[route.name];
501
562
 
502
563
  const currentId = getId?.({ params: route.params });
package/src/types.tsx CHANGED
@@ -86,21 +86,19 @@ export type Route<
86
86
  * History of param changes for this route.
87
87
  */
88
88
  history?: { type: 'params'; params: object }[] | undefined;
89
- } & Readonly<
90
- undefined extends Params
91
- ? {
92
- /**
93
- * Params for this route
94
- */
95
- params?: Readonly<Params> | undefined;
96
- }
97
- : {
98
- /**
99
- * Params for this route
100
- */
101
- params: Readonly<Params>;
102
- }
103
- >
89
+ } & (undefined extends Params
90
+ ? {
91
+ /**
92
+ * Params for this route
93
+ */
94
+ params?: Readonly<Params> | undefined;
95
+ }
96
+ : {
97
+ /**
98
+ * Params for this route
99
+ */
100
+ params: Readonly<Params>;
101
+ })
104
102
  >;
105
103
 
106
104
  export type ParamListBase = Record<string, object | undefined>;