honojs/hono · error · Error

RequestContext is not provided.

Error message

RequestContext is not provided.

What it means

useRequestContext() relies on React's useContext to retrieve the per-request Context object; it only works inside components rendered by the jsxRenderer middleware, which provides RequestContext. Calling it outside that render tree (no provider above the component) yields undefined and this error is thrown.

Source

Thrown at src/middleware/jsx-renderer/index.ts:160

 * }
 *
 * app.get('/page/info', (c) => {
 *   return c.render(
 *     <div>
 *       You are accessing: <RequestUrlBadge />
 *     </div>
 *   )
 * })
 * ```
 */
export const useRequestContext = <
  E extends Env = any,
  P extends string = any,
  I extends Input = {},
>(): Context<E, P, I> => {
  const c = useContext(RequestContext)
  if (!c) {
    throw new Error('RequestContext is not provided.')
  }
  return c
}

View on GitHub (pinned to e2740d5a1b)

Solutions

  1. Ensure the jsxRenderer middleware is applied (app.use(jsxRenderer())) before routes that render the component
  2. Render the component through c.render(...) so RequestContext is provided
  3. In tests, wrap the component with the RequestContext provider (pass a mock Context) instead of rendering bare
  4. For code shared with client rendering, pass the Context explicitly via props rather than using the hook

Example fix

// before
app.get('/page', (c) => c.html(<Layout />)) // no RequestContext
// after
app.use(jsxRenderer())
app.get('/page', (c) => c.render(<Layout />))
Defensive patterns

Strategy: validation

Validate before calling

const canUseRequestContext = (): boolean => {
  // works only when rendered via jsxRenderer; guard shared components:
  try { useRequestContext(); return true } catch { return false }
}

Try / catch

// inside shared components:
let c: Context | undefined
try { c = useRequestContext() } catch { /* rendered outside jsxRenderer; fall back to props */ }

Prevention

When it happens

Trigger: Calling useRequestContext() in a component rendered by React DOM (client-side), in a route handler outside JSX rendering, in a component rendered via JSX without the jsxRenderer middleware installed, or during SSR of a tree that jsxRenderer did not create.

Common situations: Sharing components between a Hono app and a Vite/Next client app; forgetting app.use(jsxRenderer(...)) before routes using JSX; testing components in isolation with react-testing-library without wrapping in the RequestContext provider; upgrading Hono and missing middleware ordering changes.

Related errors


AI-assisted analysis of honojs/hono@e2740d5a1b (2026-08-28). Data as JSON: /api/errors/ba315c7be05873bd. Report an issue: GitHub.