- commit
- 9f21d7e
- parent
- a29e562
- author
- Eric Bower
- date
- 2024-08-18 19:14:59 +0000 UTC
docs(loaders): meta code example
1 files changed,
+20,
-0
+20,
-0
1@@ -100,3 +100,23 @@ pass structured data from a thunk into the view on success or failure. Maybe
2 this is the new `id` for the entity you just created and the view needs to know
3 it. The `meta` prop is where you would put contextual information beyond the
4 `message` string.
5+
6+Here's an example for how you can update the `meta` property inside an endpoint:
7+
8+```tsx
9+const fetchUsers = api.get("/users", function* (ctx, next) {
10+ yield* next();
11+ if (!ctx.json.ok) return;
12+ // this will merge with the default success loader state
13+ // so you don't have to set the `status` here as it is done automatically
14+ // with the api middleware
15+ ctx.loader = { meta: { total: ctx.json.value.length } };
16+});
17+
18+function App() {
19+ const loader = useQuery(fetchUsers());
20+ if (loader.isInitialLoading) return <div>loading ...</div>;
21+ if (loader.isError) return <div>error: {loader.message}</div>;
22+ return <div>Total number of users: {loader.meta.total}</div>;
23+}
24+```