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
Copy file name to clipboardExpand all lines: docs/how-to/middleware.md
+18-26Lines changed: 18 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -224,58 +224,50 @@ let db = context.get(dbContext);
224
224
// ^ Database
225
225
```
226
226
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:
227
+
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
228
229
229
```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:
248
+
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
259
function getLoadContext() {
263
260
const loadContext = {...};
264
261
- return loadContext;
265
-
+return new Map([
266
-
+ [myLoadContext, loadContext]]
267
-
+);
262
+
+let context = new unstable_RouterContextProvider();
263
+
+Object.assign(context, loadContext);
264
+
+return context;
268
265
}
269
266
```
270
267
271
-
Update your loaders/actions to read from the new context instance:
268
+
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
269
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
-
```
270
+
<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>
0 commit comments