{"record":{"id":"88ac77066a3db095","repo":"remix-run/react-router","slug":"no-value-found-for-context","errorCode":null,"errorMessage":"No value found for context","messagePattern":"No value found for context","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/react-router/lib/router/utils.ts","lineNumber":250,"sourceCode":"\n  /**\n   * Access a value from the context. If no value has been set for the context,\n   * it will return the context's `defaultValue` if provided, or throw an error\n   * if no `defaultValue` was set.\n   * @param context The context to get the value for\n   * @returns The value for the context, or the context's `defaultValue` if no\n   * value was set\n   */\n  get<T>(context: RouterContext<T>): T {\n    if (this.#map.has(context)) {\n      return this.#map.get(context) as T;\n    }\n\n    if (context.defaultValue !== undefined) {\n      return context.defaultValue;\n    }\n\n    throw new Error(\"No value found for context\");\n  }\n\n  /**\n   * Set a value for the context. If the context already has a value set, this\n   * will overwrite it.\n   *\n   * @param context The context to set the value for\n   * @param value The value to set for the context\n   * @returns {void}\n   */\n  set<C extends RouterContext>(\n    context: C,\n    value: C extends RouterContext<infer T> ? T : never,\n  ): void {\n    this.#map.set(context, value);\n  }\n}\n","sourceCodeStart":232,"sourceCodeEnd":268,"githubUrl":"https://github.com/remix-run/react-router/blob/7aea711dd1ae2bc5a076d13ff17291829690fa74/packages/react-router/lib/router/utils.ts#L232-L268","documentation":"RouterContextProvider.get(context) (lib/router/utils.ts:250) throws when no value was ever set for that context and it was created without a defaultValue. The intended flow is: a middleware on a parent route calls context.set() early, and loaders/actions below it call context.get(); reading before any setter ran is treated as a wiring bug.","triggerScenarios":"context.get(myContext) in a loader whose middleware never ran (middleware defined on a different branch or a child of the reading route); forgot the context.set() call in the middleware; middleware ordering puts the reader above the setter in the route tree; reading in getContext() before middleware ran.","commonSituations":"Adding context reads to new routes while the middleware only matches sibling branches; refactoring moves middleware to a different route; conditional set inside middleware skipped on some paths.","solutions":["Give the context a default: createContext<User | null>(null) so get() returns it instead of throwing","Ensure the middleware that sets the value is defined on a parent route of every route that reads it","Set the value unconditionally in middleware (fallback value) before any branch that skips it","Check route nesting: the reading route must be nested under the middleware route"],"exampleFix":"// before\nexport const userContext = createContext<User>()\n// after\nexport const userContext = createContext<User | null>(null)","handlingStrategy":"validation","validationCode":"// Defensive read: provide defaults at creation so get() never throws\nexport const userContext = createContext<User | null>(null);\n// and ensure a value is always set before readers run\nexport const userMiddleware = async ({ context, request }) => {\n  context.set(userContext, (await getUser(request)) ?? null);\n};","typeGuard":null,"tryCatchPattern":"let user: User | undefined;\ntry { user = context.get(userContext) } catch (e) { if (e instanceof Error && e.message === 'No value found for context') user = undefined; else throw e }","preventionTips":["Create contexts with a defaultValue (createContext<T | null>(null)) unless absence is a bug","Define the setting middleware on a parent route above every reader","Set values unconditionally in middleware, using fallbacks in skipped branches","Document which routes are under each context-setting middleware"],"tags":["middleware","context","routing","api-misuse"],"backgroundTag":"missing-context-value","analyzedSha":"7aea711dd1ae2bc5a076d13ff17291829690fa74","analyzedAt":"2026-08-18T18:04:14.938Z","contentChangedAt":"2026-08-18T18:04:14.938Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}