Remix
Remix Framework
A React framework based on web standards. Focuses on server rendering and data loading.
Remix Framework
A React framework based on web standards. Focuses on server rendering and data loading.
Remix is a full-stack React framework developed by the React Router team (Ryan Florence, Michael Jackson), and it was acquired by Shopify in 2022. Guided by the philosophy of "going back to the basics of the web," it makes the most of the browser's default behaviors (HTML form, HTTP caching, progressive enhancement). It aims for Progressive Enhancement, in which core functionality works even without JavaScript.
The core of Remix is loader and action. A loader fetches data on the server for GET requests, and an action handles POST/PUT/DELETE requests. This is similar to the traditional MVC pattern, but it is tightly integrated with React components. Server data is accessed through the useLoaderData and useActionData hooks, and server/client code is written in the same file without a separate API endpoint.
Remix's form handling makes use of the default behavior of the HTML <form>. The Form component works even before JavaScript has loaded, and after JS loads it is enhanced with fetch. useFetcher can handle multiple mutations simultaneously, and optimistic updates (Optimistic UI) are also easy to implement. When an error occurs, the form state is preserved, which provides a good user experience.
In Remix v2 (2023), it was integrated with React Router v6.4, which further strengthened its routing patterns. Nested Routes make nesting layouts natural, and each route loads its data independently, which prevents the Waterfall problem. It supports various server runtimes such as Cloudflare Workers, Deno, and Node.js, and the development experience has been improved with Vite as its foundation.
// app/routes/posts._index.tsx
import type { LoaderFunctionArgs, ActionFunctionArgs } from '@remix-run/node';
import { json, redirect } from '@remix-run/node';
import { useLoaderData, useActionData, Form, useNavigation } from '@remix-run/react';
import { prisma } from '~/lib/prisma.server';
// loader - GET μμ² μ²λ¦¬ (λ°μ΄ν° λ‘λ)
export async function loader({ request }: LoaderFunctionArgs) {
const url = new URL(request.url);
const search = url.searchParams.get('q') || '';
const posts = await prisma.post.findMany({
where: search ? {
OR: [
{ title: { contains: search } },
{ content: { contains: search } },
]
} : undefined,
include: { author: { select: { name: true } } },
orderBy: { createdAt: 'desc' },
});
return json({ posts, search });
}
// action - POST/PUT/DELETE μ²λ¦¬ (mutation)
export async function action({ request }: ActionFunctionArgs) {
const formData = await request.formData();
const intent = formData.get('intent');
if (intent === 'delete') {
const id = formData.get('id') as string;
await prisma.post.delete({ where: { id } });
return json({ success: true });
}
if (intent === 'create') {
const title = formData.get('title') as string;
const content = formData.get('content') as string;
// μ ν¨μ± κ²μ¬
const errors: Record<string, string> = {};
if (!title) errors.title = 'μ λͺ©μ μ
λ ₯νμΈμ';
if (!content) errors.content = 'λ΄μ©μ μ
λ ₯νμΈμ';
if (Object.keys(errors).length) {
return json({ errors }, { status: 400 });
}
await prisma.post.create({
data: { title, content, authorId: 1 }
});
return redirect('/posts');
}
return json({ error: 'Invalid intent' }, { status: 400 });
}
// μ»΄ν¬λνΈ
export default function PostsIndex() {
const { posts, search } = useLoaderData<typeof loader>();
const actionData = useActionData<typeof action>();
const navigation = useNavigation();
const isCreating = navigation.formData?.get('intent') === 'create';
return (
<div className="max-w-4xl mx-auto py-8">
{/* κ²μ νΌ - GET μμ² */}
<Form method="get" className="mb-6">
<input
type="search"
name="q"
defaultValue={search}
placeholder="κ²μμ΄ μ
λ ₯..."
className="border p-2 rounded w-full"
/>
</Form>
{/* μ κΈ μμ± νΌ - POST μμ² */}
<Form method="post" className="mb-8 p-4 border rounded">
<input type="hidden" name="intent" value="create" />
<input
name="title"
placeholder="μ λͺ©"
className="w-full border p-2 rounded mb-2"
/>
{actionData?.errors?.title && (
<p className="text-red-500 text-sm">{actionData.errors.title}</p>
)}
<textarea
name="content"
placeholder="λ΄μ©"
rows={4}
className="w-full border p-2 rounded mb-2"
/>
{actionData?.errors?.content && (
<p className="text-red-500 text-sm">{actionData.errors.content}</p>
)}
<button
type="submit"
disabled={isCreating}
className="bg-blue-500 text-white px-4 py-2 rounded disabled:opacity-50"
>
{isCreating ? 'μ μ₯ μ€...' : 'κ²μκΈ μμ±'}
</button>
</Form>
{/* κ²μκΈ λͺ©λ‘ */}
<ul className="space-y-4">
{posts.map(post => (
<li key={post.id} className="p-4 border rounded flex justify-between">
<div>
<h3 className="font-bold">{post.title}</h3>
<p className="text-gray-600">{post.author.name}</p>
</div>
<Form method="post">
<input type="hidden" name="intent" value="delete" />
<input type="hidden" name="id" value={post.id} />
<button type="submit" className="text-red-500">
μμ
</button>
</Form>
</li>
))}
</ul>
</div>
);
}
// app/routes/posts.$id.tsx
import { useFetcher } from '@remix-run/react';
// μ’μμ λ²νΌ - Optimistic UI
function LikeButton({ postId, likes, isLiked }: {
postId: string;
likes: number;
isLiked: boolean;
}) {
const fetcher = useFetcher();
// λκ΄μ μ
λ°μ΄νΈ: μμ² μ€μ΄λ©΄ μμ κ²°κ³Ό νμ
const optimisticLikes = fetcher.formData
? (isLiked ? likes - 1 : likes + 1)
: likes;
const optimisticIsLiked = fetcher.formData
? !isLiked
: isLiked;
return (
<fetcher.Form method="post" action={`/posts/${postId}/like`}>
<button
type="submit"
className={`flex items-center gap-1 ${
optimisticIsLiked ? 'text-red-500' : 'text-gray-500'
}`}
disabled={fetcher.state !== 'idle'}
>
{optimisticIsLiked ? 'β€οΈ' : 'π€'}
<span>{optimisticLikes}</span>
</button>
</fetcher.Form>
);
}
// μ¬λ¬ fetcher λμ μ¬μ© μμ
function CommentSection({ postId }: { postId: string }) {
const fetcher = useFetcher();
const isSubmitting = fetcher.state === 'submitting';
return (
<div>
<fetcher.Form method="post" action={`/posts/${postId}/comments`}>
<textarea
name="content"
placeholder="λκΈμ μ
λ ₯νμΈμ..."
className="w-full border p-2 rounded"
disabled={isSubmitting}
/>
<button
type="submit"
disabled={isSubmitting}
className="mt-2 bg-blue-500 text-white px-4 py-2 rounded"
>
{isSubmitting ? 'λ±λ‘ μ€...' : 'λκΈ λ±λ‘'}
</button>
</fetcher.Form>
</div>
);
}
Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free