Skip to content

Commit 197d2fc

Browse files
MichaelDeBoeymcansh
authored andcommitted
fix: fix useActionData/useLoaderData usage (#82)
1 parent 4452dfb commit 197d2fc

File tree

120 files changed

+567
-1050
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+567
-1050
lines changed

basic/app/routes/demos/actions.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ActionFunction, MetaFunction } from "@remix-run/node";
1+
import type { ActionArgs, MetaFunction } from "@remix-run/node";
22
import { json, redirect } from "@remix-run/node";
33
import { Form, useActionData } from "@remix-run/react";
44
import { useEffect, useRef } from "react";
@@ -10,7 +10,7 @@ export const meta: MetaFunction = () => ({
1010
// When your form sends a POST, the action is called on the server.
1111
// - https://remix.run/api/conventions#action
1212
// - https://remix.run/guides/data-updates
13-
export const action: ActionFunction = async ({ request }) => {
13+
export const action = async ({ request }: ActionArgs) => {
1414
const formData = await request.formData();
1515
const answer = formData.get("answer");
1616

@@ -35,7 +35,7 @@ export const action: ActionFunction = async ({ request }) => {
3535

3636
export default function ActionsDemo() {
3737
// https://remix.run/api/remix#useactiondata
38-
const actionMessage = useActionData<string>();
38+
const actionMessage = useActionData<typeof action>();
3939
const answerRef = useRef<HTMLInputElement>(null);
4040

4141
// This form works without JavaScript, but when we have JavaScript we can make

basic/app/routes/demos/params/$id.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import type { LoaderFunction, MetaFunction } from "@remix-run/node";
1+
import type { LoaderArgs, MetaFunction } from "@remix-run/node";
22
import { json } from "@remix-run/node";
33
import { useCatch, useLoaderData } from "@remix-run/react";
44

55
// The `$` in route filenames becomes a pattern that's parsed from the URL and
66
// passed to your loaders so you can look up data.
77
// - https://remix.run/api/conventions#loader-params
8-
export const loader: LoaderFunction = async ({ params }) => {
8+
export const loader = async ({ params }: LoaderArgs) => {
99
// pretend like we're using params.id to look something up in the db
1010

1111
if (params.id === "this-record-does-not-exist") {
@@ -39,7 +39,7 @@ export const loader: LoaderFunction = async ({ params }) => {
3939
};
4040

4141
export default function ParamDemo() {
42-
const data = useLoaderData();
42+
const data = useLoaderData<typeof loader>();
4343
return (
4444
<h1>
4545
The param is <i style={{ color: "red" }}>{data.param}</i>

basic/app/routes/index.tsx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { LoaderFunction, MetaFunction } from "@remix-run/node";
1+
import type { MetaFunction } from "@remix-run/node";
22
import { json } from "@remix-run/node";
33
import { Link, useLoaderData } from "@remix-run/react";
44

@@ -11,7 +11,7 @@ type IndexData = {
1111
// you can connect to a database or run any server side code you want right next
1212
// to the component that renders it.
1313
// https://remix.run/api/conventions#loader
14-
export const loader: LoaderFunction = async () => {
14+
export const loader = async () => {
1515
const data: IndexData = {
1616
resources: [
1717
{
@@ -48,16 +48,14 @@ export const loader: LoaderFunction = async () => {
4848
};
4949

5050
// https://remix.run/api/conventions#meta
51-
export const meta: MetaFunction = () => {
52-
return {
53-
title: "Remix Starter",
54-
description: "Welcome to remix!",
55-
};
56-
};
51+
export const meta: MetaFunction = () => ({
52+
title: "Remix Starter",
53+
description: "Welcome to remix!",
54+
});
5755

5856
// https://remix.run/guides/routing#index-routes
5957
export default function Index() {
60-
const data = useLoaderData<IndexData>();
58+
const data = useLoaderData<typeof loader>();
6159

6260
return (
6361
<div className="remix__page">

bullmq-task-queue/app/routes/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type { ActionFunction } from "@remix-run/node";
1+
import type { ActionArgs } from "@remix-run/node";
22
import { json } from "@remix-run/node";
33
import { Form, useActionData, useTransition } from "@remix-run/react";
44

55
import { queue } from "~/queues/notifier.server";
66

7-
export const action: ActionFunction = async ({ request }) => {
7+
export const action = async ({ request }: ActionArgs) => {
88
const formData = await request.formData();
99
const email = formData.get("email");
1010

@@ -23,7 +23,7 @@ export const action: ActionFunction = async ({ request }) => {
2323
};
2424

2525
export default function Index() {
26-
const actionMessage = useActionData<string>();
26+
const actionMessage = useActionData<typeof action>();
2727
const transition = useTransition();
2828

2929
return (

catch-boundary/app/routes/index.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { LoaderFunction } from "@remix-run/node";
21
import { redirect } from "@remix-run/node";
32

4-
export const loader: LoaderFunction = async () => redirect("/users");
3+
export const loader = async () => redirect("/users");

catch-boundary/app/routes/users.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
1-
import type { LoaderFunction, MetaFunction } from "@remix-run/node";
1+
import type { MetaFunction } from "@remix-run/node";
22
import { json } from "@remix-run/node";
33
import { Link, Outlet, useLoaderData } from "@remix-run/react";
44

5-
import type { User } from "~/data.server";
65
import { getUsers } from "~/data.server";
76

8-
type LoaderData = {
9-
users: Array<User>;
10-
};
11-
127
export const meta: MetaFunction = () => {
138
return { title: "Users" };
149
};
1510

16-
export const loader: LoaderFunction = async () => {
11+
export const loader = async () => {
1712
const users = getUsers();
18-
return json<LoaderData>({ users });
13+
return json({ users });
1914
};
2015

2116
export default function Users() {
22-
const { users } = useLoaderData<LoaderData>();
17+
const { users } = useLoaderData<typeof loader>();
2318

2419
return (
2520
<div>

catch-boundary/app/routes/users/$userId.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
import type { LoaderFunction, MetaFunction } from "@remix-run/node";
1+
import type { LoaderArgs, MetaFunction } from "@remix-run/node";
22
import { json } from "@remix-run/node";
33
import { useCatch, useLoaderData, useParams } from "@remix-run/react";
44

5-
import type { User as UserType } from "~/data.server";
65
import { getUsers } from "~/data.server";
76

8-
interface LoaderData {
9-
user: UserType;
10-
}
11-
12-
export const meta: MetaFunction = ({ data }: { data: LoaderData | null }) => {
7+
export const meta: MetaFunction<typeof loader> = ({ data }) => {
138
// When the response is thrown for a missing user, the data will be the
149
// thrown response.
1510
if (!data || !data.user) {
@@ -18,7 +13,7 @@ export const meta: MetaFunction = ({ data }: { data: LoaderData | null }) => {
1813
return { title: data.user.name };
1914
};
2015

21-
export const loader: LoaderFunction = async ({ params }) => {
16+
export const loader = async ({ params }: LoaderArgs) => {
2217
const userId = params.userId;
2318

2419
const users = getUsers();
@@ -29,11 +24,11 @@ export const loader: LoaderFunction = async ({ params }) => {
2924
throw new Response("Not Found", { status: 404 });
3025
}
3126

32-
return json<LoaderData>({ user });
27+
return json({ user });
3328
};
3429

3530
export default function User() {
36-
const { user } = useLoaderData<LoaderData>();
31+
const { user } = useLoaderData<typeof loader>();
3732
return <div>Hi there {user.name}!</div>;
3833
}
3934

client-side-validation/app/root.tsx

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import type {
2-
ActionFunction,
3-
LinksFunction,
4-
LoaderFunction,
5-
MetaFunction,
6-
} from "@remix-run/node";
1+
import type { ActionArgs, LinksFunction, MetaFunction } from "@remix-run/node";
72
import { json } from "@remix-run/node";
83
import {
94
Links,
@@ -27,7 +22,7 @@ export const meta: MetaFunction = () => ({
2722
viewport: "width=device-width,initial-scale=1",
2823
});
2924

30-
export const action: ActionFunction = async ({ request }) => {
25+
export const action = async ({ request }: ActionArgs) => {
3126
const form = await request.formData();
3227
const message = `Successfully submitted data:
3328
- Required text: ${form.get("required-text")}
@@ -42,12 +37,7 @@ export const action: ActionFunction = async ({ request }) => {
4237
return json({ message }, { status: 200 });
4338
};
4439

45-
type LoaderData = {
46-
todayString: string;
47-
tomorrowString: string;
48-
};
49-
50-
export const loader: LoaderFunction = async () => {
40+
export const loader = async () => {
5141
const date = new Date();
5242

5343
// today string in "YYYY-MM-DD" format
@@ -66,8 +56,8 @@ export const loader: LoaderFunction = async () => {
6656
};
6757

6858
export default function App() {
69-
const actionData = useActionData();
70-
const data = useLoaderData<LoaderData>();
59+
const actionData = useActionData<typeof action>();
60+
const data = useLoaderData<typeof loader>();
7161

7262
return (
7363
<html lang="en">

collected-notes/app/routes/$slug.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
1-
import type { LoaderFunction } from "@remix-run/node";
1+
import type { LoaderArgs } from "@remix-run/node";
22
import { json } from "@remix-run/node";
33
import { useLoaderData } from "@remix-run/react";
4-
import type { HTML } from "collected-notes";
54

65
import { cn, sitePath } from "~/cn.server";
76

8-
type LoaderData = {
9-
body: HTML;
10-
};
11-
12-
export const loader: LoaderFunction = async ({ params }) => {
7+
export const loader = async ({ params }: LoaderArgs) => {
138
const slug = params.slug;
149
if (typeof slug !== "string") throw new Error("Missing slug");
1510
const { body } = await cn.body(sitePath, slug);
16-
return json<LoaderData>({ body });
11+
return json({ body });
1712
};
1813

1914
export default function Screen() {
20-
const { body } = useLoaderData<LoaderData>();
15+
const { body } = useLoaderData<typeof loader>();
2116
return (
2217
<main>
2318
<article dangerouslySetInnerHTML={{ __html: body }} />

collected-notes/app/routes/index.tsx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
import type { LoaderFunction } from "@remix-run/node";
1+
import type { LoaderArgs } from "@remix-run/node";
22
import { json } from "@remix-run/node";
33
import { Form, Link, useLoaderData, useSearchParams } from "@remix-run/react";
4-
import type { Note, Site } from "collected-notes";
54

65
import { cn, sitePath } from "~/cn.server";
76

8-
type LoaderData = {
9-
site: Site;
10-
notes: Note[];
11-
};
12-
13-
export const loader: LoaderFunction = async ({ request }) => {
7+
export const loader = async ({ request }: LoaderArgs) => {
148
const url = new URL(request.url);
159
const term = url.searchParams.get("term") || "";
1610
const page = Number(url.searchParams.get("page") || "1");
@@ -24,11 +18,11 @@ export const loader: LoaderFunction = async ({ request }) => {
2418
cn.site(sitePath),
2519
]);
2620

27-
return json<LoaderData>({ notes, site });
21+
return json({ notes, site });
2822
};
2923

3024
export default function Screen() {
31-
const { site, notes } = useLoaderData<LoaderData>();
25+
const { site, notes } = useLoaderData<typeof loader>();
3226
const [params] = useSearchParams();
3327
const term = params.get("term") || "";
3428
const page = Number(params.get("page") || "1");

0 commit comments

Comments
 (0)