{"record":{"id":"18932fe67928076f","repo":"react-navigation/react-navigation","slug":"couldn-t-find-a-route-at-index-i-1","errorCode":null,"errorMessage":"Couldn't find a route at index ${i - 1}.","messagePattern":"Couldn't find a route at index (.+?)\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/routers/src/SwitchRouter.tsx","lineNumber":98,"sourceCode":"  }\n\n  const history: RouteHistory[] = [\n    {\n      type: TYPE_ROUTE,\n      key: route.key,\n      params: backBehavior === 'fullHistory' ? route.params : undefined,\n    },\n  ];\n\n  let initialRouteIndex;\n\n  switch (backBehavior) {\n    case 'order':\n      for (let i = index; i > 0; i--) {\n        const route = routes[i - 1];\n\n        if (route == null) {\n          throw new Error(`Couldn't find a route at index ${i - 1}.`);\n        }\n\n        history.unshift({\n          type: TYPE_ROUTE,\n          key: route.key,\n        });\n      }\n\n      break;\n\n    case 'firstRoute':\n      if (index !== 0) {\n        const route = routes[0];\n\n        if (route == null) {\n          throw new Error(\"Couldn't find a route at index 0.\");\n        }\n","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/react-navigation/react-navigation/blob/ab1319d6bbf05eae8dc25e33cbf4dc56494e0f0c/packages/routers/src/SwitchRouter.tsx#L80-L116","documentation":"With backBehavior 'order', SwitchRouter walks backwards from the current index to build history entries for every preceding route (`routes[i - 1]`) and throws if any slot is missing. A hole in the routes array (or an index past the end) breaks history construction.","triggerScenarios":"backBehavior=\"order\" on a Tab/Drawer navigator combined with a state whose index is greater than the number of routes, or routes array containing sparse entries from manual state manipulation.","commonSituations":"Removing screens (feature-flagged tabs) while persisted state still references higher indexes; manually splicing state.routes for experiments; custom routers built on SwitchRouter passing bad indexes.","solutions":["Fix the state index to be within [0, routes.length - 1] before the router builds history.","Regenerate state via getRehydratedState or navigation.reset with the current screen set.","Clear persisted navigation state when the screen list changes between app versions.","Avoid mutating state.routes directly; use router APIs that keep index consistent."],"exampleFix":"// before\nconst state = persisted; // index 4, but only 3 tabs now\n// after\nconst state = persisted.index < persisted.routes.length\n  ? persisted\n  : undefined; // rebuild fresh state","handlingStrategy":"validation","validationCode":"if (!(state.index >= 0 && state.index < state.routes.length)) {\n  state = undefined; // discard corrupt state, rebuild fresh\n}","typeGuard":"function isDenseRouteState(s) {\n  return !!s && Array.isArray(s.routes) && s.routes.every(r => r != null) &&\n    s.index >= 0 && s.index < s.routes.length;\n}","tryCatchPattern":"try {\n  return router.getInitialState({ routeNames, routeParamList });\n} catch (e) {\n  if (String(e.message).includes(\"Couldn't find a route at index\")) {\n    return router.getInitialState({ routeNames: defaultRouteNames, routeParamList: {} });\n  }\n  throw e;\n}","preventionTips":["Never splice or sparse-fill state.routes manually.","Regenerate state via getRehydratedState/getInitialState after screen changes.","Bump a storage schema key when screen sets change so old state is dropped.","Validate every state index against routes bounds before use."],"tags":["react-navigation","switch-router","back-behavior","index-out-of-bounds"],"backgroundTag":"invalid-navigation-state-index","analyzedSha":"ab1319d6bbf05eae8dc25e33cbf4dc56494e0f0c","analyzedAt":"2026-08-31T14:46:19.520Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}