Skip to content

Commit 352f119

Browse files
committed
Update docs
1 parent b1d3029 commit 352f119

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed

docs/how-to/middleware.md

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -224,58 +224,50 @@ let db = context.get(dbContext);
224224
// ^ Database
225225
```
226226

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:
228228

229229
```diff
230-
+import { unstable_createContext } from "react-router";
230+
+import {
231+
+ unstable_createContext,
232+
+ unstable_RouterContextProvider,
233+
+} from "react-router";
231234
import { createDb } from "./db";
232235
+
233236
+const dbContext = unstable_createContext<Database>();
234237

235238
function getLoadContext(req, res) {
236239
- return { db: createDb() };
237-
+ const map = new Map([[dbContext, createDb()]]);
240+
+ return new unstable_RouterContextProvider(
241+
+ new Map([[dbContext, createDb()]])
242+
+ );
238243
}
239244
```
240245

241246
### Migration from `AppLoadContext`
242247

243-
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`:
244249

245-
```ts filename=app/context.ts
246-
import { unstable_createContext } from "react-router";
247-
248-
declare module "@react-router/server-runtime" {
249-
interface AppLoadContext {
250+
```diff
251+
declare module "react-router" {
252+
- interface AppLoadContext {
253+
+ interface unstable_RouterContextProvider {
250254
db: Database;
251255
user: User;
252256
}
253257
}
254258

255-
const myLoadContext =
256-
unstable_createContext<AppLoadContext>();
257-
```
258-
259-
Update your `getLoadContext` function to return a Map with the context initial value:
260-
261-
```diff filename=server.ts
262259
function getLoadContext() {
263260
const loadContext = {...};
264261
- return loadContext;
265-
+ return new Map([
266-
+ [myLoadContext, loadContext]]
267-
+ );
262+
+ let context = new unstable_RouterContextProvider();
263+
+ Object.assign(context, loadContext);
264+
+ return context;
268265
}
269266
```
270267

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`).
272269

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>
279271

280272
## Common Patterns
281273

0 commit comments

Comments
 (0)