{"record":{"id":"12f1c7a7fd5507cc","repo":"react-navigation/react-navigation","slug":"couldn-t-find-a-route-at-index-state-index-12f1c7","errorCode":null,"errorMessage":"Couldn't find a route at index ${state.index}.","messagePattern":"Couldn't find a route at index (.+?)\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/useFocusEvents.tsx","lineNumber":26,"sourceCode":"type Options<State extends NavigationState> = {\n  state: State;\n  emitter: NavigationEventEmitter<EventMapCore<State>>;\n};\n\n/**\n * Hook to take care of emitting `focus` and `blur` events.\n */\nexport function useFocusEvents<State extends NavigationState>({\n  state,\n  emitter,\n}: Options<State>) {\n  const navigation = React.use(NavigationContext);\n  const lastFocusedKeyRef = React.useRef<string | undefined>(undefined);\n\n  const currentFocusedRoute = state.routes[state.index];\n\n  if (currentFocusedRoute == null) {\n    throw new Error(`Couldn't find a route at index ${state.index}.`);\n  }\n\n  const currentFocusedKey = currentFocusedRoute.key;\n\n  // When the parent screen changes its focus state, we also need to change child's focus\n  // Coz the child screen can't be focused if the parent screen is out of focus\n  React.useEffect(\n    () =>\n      navigation?.addListener('focus', () => {\n        lastFocusedKeyRef.current = currentFocusedKey;\n        emitter.emit({ type: 'focus', target: currentFocusedKey });\n      }),\n    [currentFocusedKey, emitter, navigation]\n  );\n\n  React.useEffect(\n    () =>\n      navigation?.addListener('blur', () => {","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/react-navigation/react-navigation/blob/ab1319d6bbf05eae8dc25e33cbf4dc56494e0f0c/packages/core/src/useFocusEvents.tsx#L8-L44","documentation":"useFocusEvents derives the currently focused route as state.routes[state.index]. If that slot is undefined, the state is structurally invalid (index out of bounds or empty routes) and the hook throws rather than operating on a nonexistent route.","triggerScenarios":"Passing a navigation state to a navigator whose `routes` array is empty or whose `index` exceeds routes.length - 1 — typically via initialState, navigation.reset with hand-built state, or custom navigators producing invalid state.","commonSituations":"Custom navigator implementations computing index incorrectly; resetting with an empty routes array; restoring persisted/crafted state with a bad index after screens were removed.","solutions":["Fix state so index points to an existing route: 0 <= index < routes.length","Ensure navigation.reset is called with at least one route and correct index","Validate persisted state before passing as initialState (check routes/index shape)","If writing a custom navigator, guarantee every state update maintains a valid index"],"exampleFix":"// before\nnavigation.reset({ index: 2, routes: [ { name: 'Home' } ] }); // index out of bounds\n// after\nnavigation.reset({ index: 0, routes: [ { name: 'Home' } ] });","handlingStrategy":"validation","validationCode":"function assertValidState(state) {\n  if (!Array.isArray(state.routes) || state.routes.length === 0 ||\n      state.index < 0 || state.index >= state.routes.length) {\n    throw new Error('Invalid navigation state: bad index or empty routes');\n  }\n}\n// run before passing initialState or calling reset","typeGuard":"function hasFocusedRoute(state) {\n  return state.routes[state.index] != null;\n}","tryCatchPattern":"try {\n  navigation.reset(restoredState);\n} catch (e) {\n  if (e.message.startsWith(\"Couldn't find a route at index\")) {\n    navigation.reset({ index: 0, routes: [{ name: 'Home' }] });\n  }\n}","preventionTips":["Invariant: 0 <= index < routes.length on every state you construct or restore","Never reset with an empty routes array","Validate persisted state before initialState","Custom navigators: unit-test index maintenance across all actions"],"tags":["navigation","focus","state-validation"],"backgroundTag":"invalid-navigation-state","analyzedSha":"ab1319d6bbf05eae8dc25e33cbf4dc56494e0f0c","analyzedAt":"2026-08-31T14:46:19.520Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}