본문 바로가기

React.js

[React 19] useFormStatus로 더 똑똑한 폼 UI 만들기

들어가며

폼을 다루다 보면 로딩 상태 관리가 늘 골치 아프죠?
버튼은 비활성화하고, 스피너는 보여주고... 이런 보일러플레이트 코드에 지치셨다면,
React 19의 `useFormStatus`가 여러분의 구원자가 될 거예요! 😊

useFormStatus란? 🤔

`useFormStatus`는 React의 Form Action과 함께 사용되는 훅으로, 폼의 제출 상태를 자동으로 추적합니다.
더 이상 로딩 상태를 수동으로 관리할 필요가 없어요!

기본 사용법 📝

```tsx
import { useFormStatus } from 'react-dom';

function SubmitButton() {
const { pending, data, method } = useFormStatus();

return (
<button
type="submit"
disabled={pending}
className={pending ? 'opacity-50' : ''}
>
{pending ? '제출 중...' : '제출하기'}

);
}

function SignupForm() {
async function handleSignup(formData: FormData) {
'use server';
// 서버 로직
}

return (






);
}
```

실전 활용 예시 💡

1. 로딩 상태에 따른 UI 변화

```tsx
function LoadingButton() {
const { pending } = useFormStatus();

return (
<button
type="submit"
className={`
flex items-center gap-2
${pending ? 'bg-gray-400' : 'bg-blue-500'}
text-white px-4 py-2 rounded
`}
disabled={pending}
>
{pending && (

{/* 스피너 SVG */}

)}
{pending ? '처리 중...' : '저장하기'}

);
}
```

2. 진행 상태 표시기

```tsx
function ProgressIndicator() {
const { pending } = useFormStatus();

return (


{pending && (
<div
className="absolute top-0 left-0 h-full bg-blue-500
animate-progress rounded-full"
style={{ width: '100%' }}
/>
)}

);
}

function UploadForm() {
async function handleUpload(formData: FormData) {
'use server';
// 파일 업로드 로직
}

return (






);
}
```

3. 폼 필드 비활성화 관리

```tsx
function SmartForm() {
const { pending } = useFormStatus();

return (
<fieldset disabled={pending} className={pending ? 'opacity-50' : ''}>