Skip to content

Can't pass context using getLoadContext #28

@omariyassinee

Description

@omariyassinee

Bug Report: getLoadContext incompatible with React Router v7+ RouterContextProvider

Description

The getLoadContext function in the template returns a plain object, which is incompatible with React Router v7.9+ that requires returning an instance of RouterContextProvider when middleware is enabled.

According to React Router v7+ documentation, when middleware is enabled, getLoadContext must return a RouterContextProvider instance:

import { RouterContextProvider } from 'react-router';

const someContext = createContext<string | null>(null)

getLoadContext(context) {
  const ctx = new RouterContextProvider();
  ctx.set(someContext, 'value');
  return ctx;
}

Problem with Current Behavior

When attempting to return a RouterContextProvider instance (even while ignoring TypeScript errors), the context values are not properly set. When trying to access the context in server loaders or actions, the values are set to their initial values null, as if the context was never changed.

Environment

  • elysia-react-router: ^0.4.4
  • react-router: ^7.9.5
  • @react-router/node: ^7.9.5
  • @react-router/serve: ^7.9.5
  • elysia: ^1.4.13
  • vite: ^7.1.12
  • Runtime: Bun v1.3.1+

Steps to Reproduce

  1. Set up elysia-react-router with React Router v7.9+
  2. Configure getLoadContext to return a plain object
  3. Observe the runtime error about invalid context
  4. Try to fix by returning RouterContextProvider instance
  5. Access context in a route loader/action
  6. Observe that context still have the initial value.

Ps: ai generated report :>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions