Skip to content

Commit

Permalink
docs(guides/data-loading): fix type inference example (remix-run#3144)
Browse files Browse the repository at this point in the history
* docs(guides/data-loading): correct type inference code example

* chore: update contributors.yml
  • Loading branch information
konradkalemba authored and aaronpowell committed May 12, 2022
1 parent 3922293 commit 0918968
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@
- kimdontdoit
- klauspaiva
- knowler
- konradkalemba
- kubaprzetakiewicz
- kuldar
- kumard3
Expand Down
19 changes: 14 additions & 5 deletions docs/guides/data-loading.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,26 +186,35 @@ export default function ProductCategory() {
If you are using TypeScript, you can use type inference to use Prisma Client generated types on when calling `useLoaderData`. This allows better type safety and intellisense when writing your code that uses the loaded data.

```tsx filename=tsx filename=app/routes/products/$productId.tsx
import type { LoaderFunction } from "@remix-run/node"; // or "@remix-run/cloudflare"
import { json } from "@remix-run/node"; // or "@remix-run/cloudflare"
import { useLoaderData } from "@remix-run/react";

import { db } from "~/db.server";

type LoaderData = Awaited<ReturnType<typeof getLoaderData>>;

async function getLoaderData() {
const products = await db.product.findMany({
async function getLoaderData(productId: string) {
const product = await db.product.findUnique({
where: {
id: productId,
},
select: {
id: true,
name: true,
imgSrc: true,
},
});
return { products };

return product;
}

export const loader = async () => {
return json<LoaderData>(await getLoaderData());
export const loader: LoaderFunction = async ({
params,
}) => {
return json<LoaderData>(
await getLoaderData(params.productId)
);
};

export default function Product() {
Expand Down

0 comments on commit 0918968

Please sign in to comment.