Skip to content

Commit d8d808c

Browse files
committed
Update docs
1 parent 63bc296 commit d8d808c

File tree

1 file changed

+28
-31
lines changed

1 file changed

+28
-31
lines changed

docs/how-to/middleware.md

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,22 @@ export default function Dashboard({
107107

108108
#### 4. Update your `getLoadContext` function (if applicable)
109109

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

112112
```diff
113-
+import { unstable_createContext } from "react-router";
113+
+import {
114+
+ unstable_createContext,
115+
+ unstable_RouterContextProvider,
116+
+} from "react-router";
114117
import { createDb } from "./db";
115-
+
118+
116119
+const dbContext = unstable_createContext<Database>();
117120

118121
function getLoadContext(req, res) {
119122
- return { db: createDb() };
120-
+ const map = new Map([[dbContext, createDb()]]);
123+
+ return new unstable_RouterContextProvider(
124+
+ new Map([[dbContext, createDb()]])
125+
+ );
121126
}
122127
```
123128

@@ -224,58 +229,50 @@ let db = context.get(dbContext);
224229
// ^ Database
225230
```
226231

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

229234
```diff
230-
+import { unstable_createContext } from "react-router";
235+
+import {
236+
+ unstable_createContext,
237+
+ unstable_RouterContextProvider,
238+
+} from "react-router";
231239
import { createDb } from "./db";
232-
+
240+
233241
+const dbContext = unstable_createContext<Database>();
234242

235243
function getLoadContext(req, res) {
236244
- return { db: createDb() };
237-
+ const map = new Map([[dbContext, createDb()]]);
245+
+ return new unstable_RouterContextProvider(
246+
+ new Map([[dbContext, createDb()]])
247+
+ );
238248
}
239249
```
240250

241251
### Migration from `AppLoadContext`
242252

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

245-
```ts filename=app/context.ts
246-
import { unstable_createContext } from "react-router";
247-
248-
declare module "@react-router/server-runtime" {
249-
interface AppLoadContext {
255+
```diff
256+
declare module "react-router" {
257+
- interface AppLoadContext {
258+
+ interface unstable_RouterContextProvider {
250259
db: Database;
251260
user: User;
252261
}
253262
}
254263

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
262264
function getLoadContext() {
263265
const loadContext = {...};
264266
- return loadContext;
265-
+ return new Map([
266-
+ [myLoadContext, loadContext]]
267-
+ );
267+
+ let context = new unstable_RouterContextProvider();
268+
+ Object.assign(context, loadContext);
269+
+ return context;
268270
}
269271
```
270272

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

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

280277
## Common Patterns
281278

0 commit comments

Comments
 (0)