Remix Page Life Cycle

This documentation will guide you through the Remix page life cycle, explaining the flow of data from the LoaderFunction to the React component, how the Form component works, and how the ActionFunction accepts data from a Form submission.

Prerequisites

Make sure you have the following libraries imported in your code:

import { Link, Form, useLoaderData, useActionData } from "@remix-run/react";
import type { ActionFunction, LoaderFunction } from "@remix-run/server-runtime";
import { json } from "@remix-run/server-runtime";

LoaderFunction and useLoaderData

The LoaderFunction is responsible for fetching data for a specific route. This data is then passed to the React component through the useLoaderData hook.

export const loader: LoaderFunction = async () => {
  const message = "Welcome to the Remix page life cycle!";
  return json({ message });
};

export default function MyComponent() {
  const data = useLoaderData();
  return <div>{data.message}</div>;
}

In the example above, the LoaderFunction returns a JSON object with a message. The React component then uses the useLoaderData hook to access the data returned by the LoaderFunction and renders the message.

Form Component

The Form component from "@remix-run/react" is used to submit data to the server. It automatically handles form submissions and routes the data to the appropriate ActionFunction.

export default function MyComponent() {
  const data = useLoaderData();
  return (
    <div>
      <h1>{data.message}</h1>
      <Form method="post">
        <input type="text" name="name" placeholder="Enter your name" required />
        <button type="submit">Submit</button>
      </Form>
    </div>
  );
}

In this example, the Form component is added to the React component. When the form is submitted, the data will be sent to the server using a POST request.

ActionFunction and useActionData

The ActionFunction is responsible for handling form submissions and processing the data. It can return a response that can be accessed in the React component using the useActionData hook.

export const action: ActionFunction = async ({ request }) => {
  const formData = await request.formData();
  const name = formData.get("name");
  return json({ submittedName: name });
};

export default function MyComponent() {
  const loaderData = useLoaderData();
  const actionData = useActionData();
  return (
    <div>
      <h1>{loaderData.message}</h1>
      <Form method="post">
        <input type="text" name="name" placeholder="Enter your name" required />
        <button type="submit">Submit</button>
      </Form>
      {actionData && <p>You submitted: {actionData.submittedName}</p>}
    </div>
  );
}

In this example, the ActionFunction processes the submitted form data and returns a JSON object with the submitted name. The React component then uses the useActionData hook to access the data returned by the ActionFunction. If there is data available, it displays the submitted name.

By understanding the flow of data from the LoaderFunction to the React component, how the Form component works, and how the ActionFunction handles form submissions, you can create powerful applications using the Remix framework.

Last updated