Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix useActionData/useLoaderData usage #82

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions basic/app/routes/demos/actions.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ActionFunction, MetaFunction } from "@remix-run/node";
import type { ActionArgs, MetaFunction } from "@remix-run/node";
import { json, redirect } from "@remix-run/node";
import { Form, useActionData } from "@remix-run/react";
import { useEffect, useRef } from "react";
Expand All @@ -10,7 +10,7 @@ export const meta: MetaFunction = () => ({
// When your form sends a POST, the action is called on the server.
// - https://remix.run/api/conventions#action
// - https://remix.run/guides/data-updates
export const action: ActionFunction = async ({ request }) => {
export const action = async ({ request }: ActionArgs) => {
const formData = await request.formData();
const answer = formData.get("answer");

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

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

// This form works without JavaScript, but when we have JavaScript we can make
Expand Down
6 changes: 3 additions & 3 deletions basic/app/routes/demos/params/$id.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { LoaderFunction, MetaFunction } from "@remix-run/node";
import type { LoaderArgs, MetaFunction } from "@remix-run/node";
import { json } from "@remix-run/node";
import { useCatch, useLoaderData } from "@remix-run/react";

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

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

export default function ParamDemo() {
const data = useLoaderData();
const data = useLoaderData<typeof loader>();
return (
<h1>
The param is <i style={{ color: "red" }}>{data.param}</i>
Expand Down
16 changes: 7 additions & 9 deletions basic/app/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LoaderFunction, MetaFunction } from "@remix-run/node";
import type { MetaFunction } from "@remix-run/node";
import { json } from "@remix-run/node";
import { Link, useLoaderData } from "@remix-run/react";

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

// https://remix.run/api/conventions#meta
export const meta: MetaFunction = () => {
return {
title: "Remix Starter",
description: "Welcome to remix!",
};
};
export const meta: MetaFunction = () => ({
title: "Remix Starter",
description: "Welcome to remix!",
});

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

return (
<div className="remix__page">
Expand Down
6 changes: 3 additions & 3 deletions bullmq-task-queue/app/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { ActionFunction } from "@remix-run/node";
import type { ActionArgs } from "@remix-run/node";
import { json } from "@remix-run/node";
import { Form, useActionData, useTransition } from "@remix-run/react";

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

export const action: ActionFunction = async ({ request }) => {
export const action = async ({ request }: ActionArgs) => {
const formData = await request.formData();
const email = formData.get("email");

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

export default function Index() {
const actionMessage = useActionData<string>();
const actionMessage = useActionData<typeof action>();
const transition = useTransition();

return (
Expand Down
3 changes: 1 addition & 2 deletions catch-boundary/app/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { LoaderFunction } from "@remix-run/node";
import { redirect } from "@remix-run/node";

export const loader: LoaderFunction = async () => redirect("/users");
export const loader = async () => redirect("/users");
13 changes: 4 additions & 9 deletions catch-boundary/app/routes/users.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import type { LoaderFunction, MetaFunction } from "@remix-run/node";
import type { MetaFunction } from "@remix-run/node";
import { json } from "@remix-run/node";
import { Link, Outlet, useLoaderData } from "@remix-run/react";

import type { User } from "~/data.server";
import { getUsers } from "~/data.server";

type LoaderData = {
users: Array<User>;
};

export const meta: MetaFunction = () => {
return { title: "Users" };
};

export const loader: LoaderFunction = async () => {
export const loader = async () => {
const users = getUsers();
return json<LoaderData>({ users });
return json({ users });
};

export default function Users() {
const { users } = useLoaderData<LoaderData>();
const { users } = useLoaderData<typeof loader>();

return (
<div>
Expand Down
15 changes: 5 additions & 10 deletions catch-boundary/app/routes/users/$userId.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import type { LoaderFunction, MetaFunction } from "@remix-run/node";
import type { LoaderArgs, MetaFunction } from "@remix-run/node";
import { json } from "@remix-run/node";
import { useCatch, useLoaderData, useParams } from "@remix-run/react";

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

interface LoaderData {
user: UserType;
}

export const meta: MetaFunction = ({ data }: { data: LoaderData | null }) => {
export const meta: MetaFunction<typeof loader> = ({ data }) => {
// When the response is thrown for a missing user, the data will be the
// thrown response.
if (!data || !data.user) {
Expand All @@ -18,7 +13,7 @@ export const meta: MetaFunction = ({ data }: { data: LoaderData | null }) => {
return { title: data.user.name };
};

export const loader: LoaderFunction = async ({ params }) => {
export const loader = async ({ params }: LoaderArgs) => {
const userId = params.userId;

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

return json<LoaderData>({ user });
return json({ user });
};

export default function User() {
const { user } = useLoaderData<LoaderData>();
const { user } = useLoaderData<typeof loader>();
return <div>Hi there {user.name}!</div>;
}

Expand Down
20 changes: 5 additions & 15 deletions client-side-validation/app/root.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import type {
ActionFunction,
LinksFunction,
LoaderFunction,
MetaFunction,
} from "@remix-run/node";
import type { ActionArgs, LinksFunction, MetaFunction } from "@remix-run/node";
import { json } from "@remix-run/node";
import {
Links,
Expand All @@ -27,7 +22,7 @@ export const meta: MetaFunction = () => ({
viewport: "width=device-width,initial-scale=1",
});

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

type LoaderData = {
todayString: string;
tomorrowString: string;
};

export const loader: LoaderFunction = async () => {
export const loader = async () => {
const date = new Date();

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

export default function App() {
const actionData = useActionData();
const data = useLoaderData<LoaderData>();
const actionData = useActionData<typeof action>();
const data = useLoaderData<typeof loader>();

return (
<html lang="en">
Expand Down
13 changes: 4 additions & 9 deletions collected-notes/app/routes/$slug.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import type { LoaderFunction } from "@remix-run/node";
import type { LoaderArgs } from "@remix-run/node";
import { json } from "@remix-run/node";
import { useLoaderData } from "@remix-run/react";
import type { HTML } from "collected-notes";

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

type LoaderData = {
body: HTML;
};

export const loader: LoaderFunction = async ({ params }) => {
export const loader = async ({ params }: LoaderArgs) => {
const slug = params.slug;
if (typeof slug !== "string") throw new Error("Missing slug");
const { body } = await cn.body(sitePath, slug);
return json<LoaderData>({ body });
return json({ body });
};

export default function Screen() {
const { body } = useLoaderData<LoaderData>();
const { body } = useLoaderData<typeof loader>();
return (
<main>
<article dangerouslySetInnerHTML={{ __html: body }} />
Expand Down
14 changes: 4 additions & 10 deletions collected-notes/app/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import type { LoaderFunction } from "@remix-run/node";
import type { LoaderArgs } from "@remix-run/node";
import { json } from "@remix-run/node";
import { Form, Link, useLoaderData, useSearchParams } from "@remix-run/react";
import type { Note, Site } from "collected-notes";

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

type LoaderData = {
site: Site;
notes: Note[];
};

export const loader: LoaderFunction = async ({ request }) => {
export const loader = async ({ request }: LoaderArgs) => {
const url = new URL(request.url);
const term = url.searchParams.get("term") || "";
const page = Number(url.searchParams.get("page") || "1");
Expand All @@ -24,11 +18,11 @@ export const loader: LoaderFunction = async ({ request }) => {
cn.site(sitePath),
]);

return json<LoaderData>({ notes, site });
return json({ notes, site });
};

export default function Screen() {
const { site, notes } = useLoaderData<LoaderData>();
const { site, notes } = useLoaderData<typeof loader>();
const [params] = useSearchParams();
const term = params.get("term") || "";
const page = Number(params.get("page") || "1");
Expand Down
4 changes: 2 additions & 2 deletions combobox-resource-route/app/routes/lang-search.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LoaderFunction } from "@remix-run/node";
import type { LoaderArgs } from "@remix-run/node";
import { json } from "@remix-run/node";

import { searchLangs } from "~/models/langs";
Expand All @@ -8,7 +8,7 @@ import { searchLangs } from "~/models/langs";
* set of languages as the user types. It's called a Resource Route because it
* doesn't export a component. You might think of it as an "API Route".
*/
export const loader: LoaderFunction = async ({ request }) => {
export const loader = async ({ request }: LoaderArgs) => {
// First get what the user is searching for by creating a URL:
// https://developer.mozilla.org/en-US/docs/Web/API/URL
// https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
Expand Down
20 changes: 7 additions & 13 deletions dark-mode/app/root.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { LoaderFunction, MetaFunction } from "@remix-run/node";
import type { LoaderArgs, MetaFunction } from "@remix-run/node";
import { json } from "@remix-run/node";
import {
Links,
LiveReload,
Expand All @@ -15,21 +16,14 @@ import {
ThemeProvider,
useTheme,
} from "~/utils/theme-provider";
import type { Theme } from "~/utils/theme-provider";
import { getThemeSession } from "~/utils/theme.server";

export type LoaderData = {
theme: Theme | null;
};

export const loader: LoaderFunction = async ({ request }) => {
export const loader = async ({ request }: LoaderArgs) => {
const themeSession = await getThemeSession(request);

const data: LoaderData = {
return json({
theme: themeSession.getTheme(),
};

return data;
});
};

export const meta: MetaFunction = () => ({
Expand All @@ -39,7 +33,7 @@ export const meta: MetaFunction = () => ({
});

function App() {
const data = useLoaderData<LoaderData>();
const data = useLoaderData<typeof loader>();
const [theme] = useTheme();

return (
Expand All @@ -61,7 +55,7 @@ function App() {
}

export default function AppWithProviders() {
const data = useLoaderData<LoaderData>();
const data = useLoaderData<typeof loader>();

return (
<ThemeProvider specifiedTheme={data.theme}>
Expand Down
6 changes: 3 additions & 3 deletions dark-mode/app/routes/action/set-theme.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { ActionFunction, LoaderFunction } from "@remix-run/node";
import type { ActionArgs } from "@remix-run/node";
import { json, redirect } from "@remix-run/node";

import { getThemeSession } from "~/utils/theme.server";
import { isTheme } from "~/utils/theme-provider";

export const action: ActionFunction = async ({ request }) => {
export const action = async ({ request }: ActionArgs) => {
const themeSession = await getThemeSession(request);
const requestText = await request.text();
const form = new URLSearchParams(requestText);
Expand All @@ -24,4 +24,4 @@ export const action: ActionFunction = async ({ request }) => {
);
};

export const loader: LoaderFunction = () => redirect("/", { status: 404 });
export const loader = async () => redirect("/", { status: 404 });
4 changes: 2 additions & 2 deletions dataloader/app/data.server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export interface User {
interface User {
id: string;
email: string;
name: string;
}

export const users: User[] = [
const users: User[] = [
{
id: "ef3fcb93-0623-4d10-adbf-4dd865d6688c",
email: "Melisa_Treutel38@gmail.com",
Expand Down
3 changes: 1 addition & 2 deletions dataloader/app/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { LoaderFunction } from "@remix-run/node";
import { redirect } from "@remix-run/node";

export const loader: LoaderFunction = async () => redirect("/users");
export const loader = async () => redirect("/users");
Loading