{"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":"Error","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/1fd704a7dabcbe3ae09d7387b460e6acaba30ec1/packages/react-router/lib/router/utils.ts#L232-L268","documentation":"`RouterContextProvider.get(context)` throws when no value has been set for the requested `RouterContext` AND the context has no `defaultValue`. The provider stores values in a private map; on miss it falls back to `context.defaultValue`, and only throws if that is also undefined.","triggerScenarios":"Calling `context.get(SomeContext)` from a loader/action/middleware for a context that was never set by an upstream middleware or by the server's getLoadContext, and whose `RouterContext` was created without a default.","commonSituations":"Forgetting to declare/set a shared context in a root middleware before a child route reads it; rename or refactor that changes the context object identity so `get` no longer finds the set value; reading context before the provider has been populated in the request lifecycle.","solutions":["Ensure an upstream middleware (or `getLoadContext`) calls `context.set(MyContext, value)` before any downstream code calls `get(MyContext)`.","Create the `RouterContext` with a `defaultValue` so reads never throw.","Confirm all readers reference the exact same `RouterContext` instance the writer used (module identity)."],"exampleFix":"// before\nconst Ctx = createRouterContext<string>(); // no default\n// in a loader:\ncontext.get(Ctx); // throws\n\n// after (option A: default)\nconst Ctx = createRouterContext<string>('guest');\n// after (option B: set upstream)\nexport const middleware = async ({ context }, next) => {\n  context.set(Ctx, 'user-123');\n  return next();\n};","handlingStrategy":"validation","validationCode":"function hasContextValue<T>(p: RouterContextProvider, c: RouterContext<T>): boolean {\n  try { p.get(c); return true; } catch { return false; }\n}\nif (!hasContextValue(context, MyContext)) context.set(MyContext, defaultValue);","typeGuard":"function tryGetContext<T>(p: RouterContextProvider, c: RouterContext<T>): T | undefined {\n  try { return p.get(c); } catch { return undefined; }\n}","tryCatchPattern":"let value: T;\ntry {\n  value = context.get(MyContext);\n} catch {\n  value = fallbackValue;\n}","preventionTips":["Create contexts with a `defaultValue` when a fallback is acceptable.","Set context in the topmost middleware before any reader runs.","Import the same `RouterContext` instance from a single module."],"tags":["context","router","middleware","state"],"backgroundTag":null,"analyzedSha":"1fd704a7dabcbe3ae09d7387b460e6acaba30ec1","analyzedAt":"2026-08-12T13:54:57.804Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}