You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[UNSTABLE] When middleware is enabled, make the `context` parameter read-only (via `Readonly<unstable_RouterContextProvider>`) so that TypeScript will not allow you to write arbitrary fields to it in loaders, actions, or middleware.
[UNSTABLE] Change `getLoadContext` signature (`type GetLoadContextFunction`) when `future.unstable_middleware` is enabled so that it returns an `unstable_RouterContextProvider` instance instead of a `Map` used to contruct the instance internally
10
+
11
+
- This also removes the `type unstable_InitialContext` export
12
+
- ⚠️ This is a breaking change if you have adopted middleware and are using a custom server with a `getLoadContext` function
[UNSTABLE] Change the `unstable_getContext` signature on `RouterProvider`/`HydratedRouter`/`unstable_RSCHydratedRouter` so that it returns an `unstable_RouterContextProvider` instance instead of a `Map` used to contruct the instance internally
6
+
7
+
- ⚠️ This is a breaking change if you have adopted the `unstable_getContext` prop
Copy file name to clipboardExpand all lines: docs/how-to/middleware.md
+30-31Lines changed: 30 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -107,17 +107,22 @@ export default function Dashboard({
107
107
108
108
#### 4. Update your `getLoadContext` function (if applicable)
109
109
110
-
If you're using a custom server and a `getLoadContext` function, you will need to update your implementation to return a Map of contexts and values, instead of a JavaScript object:
110
+
If you're using a custom server and a `getLoadContext` function, you will need to update your implementation to return an instance of `unstable_RouterContextProvider`, instead of a JavaScript object:
111
111
112
112
```diff
113
-
+import { unstable_createContext } from "react-router";
@@ -224,58 +229,52 @@ let db = context.get(dbContext);
224
229
// ^ Database
225
230
```
226
231
227
-
If you're using a custom server and a `getLoadContext` function, you will need to update your implementation to return a `Map` of contexts and values, instead of a JavaScript object:
232
+
If you're using a custom server and a `getLoadContext` function, you will need to update your implementation to return an instance of `unstable_RouterContextProvider`, instead of a plain JavaScript object:
228
233
229
234
```diff
230
-
+import { unstable_createContext } from "react-router";
If you're currently using `AppLoadContext`, you can migrate most easily by creating a context for your existing object:
253
+
If you're currently using `AppLoadContext`, you can migrate incrementally by using your existing module augmentation to augment `unstable_RouterContextProvider` instead of `AppLoadContext`. Then, update your `getLoadContext` function to return an instance of `unstable_RouterContextProvider`:
Update your `getLoadContext` function to return a Map with the context initial value:
260
-
261
-
```diff filename=server.ts
262
264
function getLoadContext() {
263
265
const loadContext = {...};
264
266
- return loadContext;
265
-
+return new Map([
266
-
+ [myLoadContext, loadContext]]
267
-
+);
267
+
+let context = new unstable_RouterContextProvider();
268
+
+Object.assign(context, loadContext);
269
+
+return context;
268
270
}
269
271
```
270
272
271
-
Update your loaders/actions to read from the new context instance:
273
+
This allows you to leave your loaders/actions untouched during initial adoption of middleware, since they can still read values directly (i.e., `context.db`).
272
274
273
-
```diff filename=app/routes/example.tsx
274
-
export function loader({ context }: Route.LoaderArgs) {
275
-
- const { db, user } = context;
276
-
+ const { db, user } = context.get(myLoadContext);
277
-
}
278
-
```
275
+
<docs-warning>This approach is only intended to be used as a migration strategy when adopting middleware in React Router v7, allowing you to incrementally migrate to `context.set`/`context.get`. It is not safe to assume this approach will work in the next major version of React Router.</docs-warning>
276
+
277
+
<docs-warning>The `unstable_RouterContextProvider` class is also used for the client-side `context` parameter via `<HydratedRouter unstable_getContext>` and `<RouterProvider unstable_getContext>`. Since `AppLoadContext` is primarily intended as a hand-off from your HTTP server into the React Router handlers, you need to be aware that these augmented fields will not be available in `clientMiddleware`, `clientLoader`, or `clientAction` functions even thought TypeScript will tell you they are (unless, of course, you provide the fields via `unstable_getContext` on the client).</docs-warning>
0 commit comments