| title | <form> |
|---|
λ΄μ₯ λΈλΌμ°μ <form> μ»΄ν¬λνΈλ‘ μ 보 μ μΆμ μν λνν 컨νΈλ‘€μ λ§λ€ μ μμ΅λλ€.
<form action={search}>
<input name="query" />
<button type="submit">κ²μ</button>
</form>μ 보 μ μΆμ μν λνν 컨νΈλ‘€μ μμ±νκΈ° μν΄, λ΄μ₯ λΈλΌμ°μ <form> μ»΄ν¬λνΈλ₯Ό λ λλ§νμΈμ.
<form action={search}>
<input name="query" />
<button type="submit">κ²μ</button>
</form>μλ μμλ₯Ό μ°Έκ³ νμΈμ.
<form>μ λͺ¨λ κ³΅ν΅ μλ¦¬λ¨ΌνΈ Propsλ₯Ό μ§μν©λλ€.
action: a URL or function. When a URL is passed to action the form will behave like the HTML form component. When a function is passed to action the function will handle the form submission in a Transition following the Action prop pattern. The function passed to action may be async and will be called with a single argument containing the form data of the submitted form. The action prop can be overridden by a formAction attribute on a <button>, <input type="submit">, or <input type="image"> component.
- ν¨μλ₯Ό
actionμ΄λformActionμ μ λ¬νλ©΄, HTTP λ©μλλmethodνλ‘νΌν°μ κ°κ³Ό κ΄κ³μμ΄ POSTλ‘ μ²λ¦¬ν©λλ€.
<<<<<<< HEAD
νΌμ΄ μ μΆλ λ ν¨μλ₯Ό μ€ννκΈ° μν΄, νΌμ action νλ‘νΌν°μ ν¨μλ₯Ό μ λ¬νμΈμ. formDataκ° ν¨μμ μΈμλ‘ μ λ¬λμ΄, νΌμμ μ λ¬λ λ°μ΄ν°μ μ κ·Όν μ μμ΅λλ€. μ΄ μ μ΄ URLλ§ λ°λ κΈ°μ‘΄ HTML actionκ³Όμ μ°¨μ΄μ μ
λλ€. After the action function succeeds, all uncontrolled field elements in the form are reset.
Pass a function to the onSubmit event handler to run code when the form is submitted. By default, the browser sends the form data to the current URL and refreshes the page, so call e.preventDefault() to override that behavior.
This example reads the submitted values with new FormData(e.target), which collects every field by its name. This keeps the inputs uncontrolled. If you instead control an input with state, read from that state on submit rather than from FormData.
export default function Search() {
function handleSubmit(e) {
// Prevent the browser from reloading the page
e.preventDefault();
// Read the form data
const form = e.target;
const formData = new FormData(form);
const query = formData.get("query");
alert(`You searched for '${query}'`);
}
return (
<form onSubmit={handleSubmit}>
<input name="query" />
<button type="submit">Search</button>
</form>
);
}Reading form data with onSubmit works in every version of React and gives you direct access to the submit event, so you can call e.preventDefault() and read the data yourself. Passing the function to the action prop instead runs the submission in a Transition. React then tracks the pending state, sends thrown errors to the nearest error boundary, and lets the form work with useActionState and useOptimistic. An action can also be a Server Function, which onSubmit does not support.
Pass a function to the action prop of form to run the function when the form is submitted. formData will be passed to the function as an argument so you can access the data submitted by the form. This differs from the conventional HTML action, which only accepts URLs. Unlike onSubmit, an action runs in a Transition and calling e.preventDefault() isn't needed. After the action function succeeds, all uncontrolled field elements in the form are reset.
152a471aa9ac2f6f0f3e64c04f39da790d40cf61
export default function Search() {
function search(formData) {
const query = formData.get("query");
alert(`'${query}'μ(λ₯Ό) κ²μνμ΅λλ€.`);
}
return (
<form action={search}>
<input name="query" />
<button type="submit">κ²μ</button>
</form>
);
}μ
λ ₯ λ° μ μΆ λ²νΌκ³Ό ν¨κ» <form>μ λ λλ§νμΈμ. νΌμ μ μΆν λ ν΄λΉ ν¨μλ₯Ό μ€ννκΈ° μν΄ μλ² ν¨μ('use server'κ° νμλ ν¨μ)λ₯Ό νΌμ action νλ‘νΌν°λ‘ μ λ¬νμΈμ.
<form action>μ μλ² ν¨μλ₯Ό μ λ¬νλ©΄ μλ°μ€ν¬λ¦½νΈκ° νμ±νλκΈ° μ μ΄λ μ½λκ° λ‘λλκΈ° μ μ μ¬μ©μκ° νΌμ μ μΆν μ μμ΅λλ€. μ΄λ μ°κ²° μνλ κΈ°κ³κ° λ리거λ μλ°μ€ν¬λ¦½νΈκ° λΉνμ±νλ μ¬μ©μμκ² μ μ©νκ³ , action νλ‘νΌν°μ URLμ΄ μ λ¬λ λμ νΌμ΄ λμνλ λ°©μμ λΉμ·ν©λλ€.
<form>μ μ‘μ
μ λ°μ΄ν°λ₯Ό μ 곡νκΈ° μν΄ νΌ νλμ hiddenμ μ¬μ©ν μ μμ΅λλ€. μλ² ν¨μλ formData λμ hiddenμ΄ μ μ©λ νΌ νλ λ°μ΄ν°λ₯Ό λΆλ¬μ¬ μ μμ΅λλ€.
import { updateCart } from './lib.js';
function AddToCart({productId}) {
async function addToCart(formData) {
'use server'
const productId = formData.get('productId')
await updateCart(productId)
}
return (
<form action={addToCart}>
<input type="hidden" name="productId" value={productId} />
<button type="submit">μ₯λ°κ΅¬λμ μΆκ°</button>
</form>
);
}νΌ μ‘μ
μ λ°λ₯Έ λ°μ΄ν°λ₯Ό μ 곡νκΈ° μν΄ hidden νΌ νλλ₯Ό μ¬μ©νλ λμ μ bindλ₯Ό νΈμΆν΄ μΆκ° μΈμλ₯Ό μ 곡ν μ μμ΅λλ€. μ΄λ κ² νλ©΄ ν¨μμ μΈμλ‘ μ λ¬λλ formData μΈμ μ μΈμ(productId)κ° ν¨μμ λ°μΈλ©λ©λλ€.
import { updateCart } from './lib.js';
function AddToCart({productId}) {
async function addToCart(productId, formData) {
"use server";
await updateCart(productId)
}
const addProductToCart = addToCart.bind(null, productId);
return (
<form action={addProductToCart}>
<button type="submit">μ₯λ°κ΅¬λμ μΆκ°</button>
</form>
);
}<form>μ΄ μλ² μ»΄ν¬λνΈμ μν΄ λ λλ§λκ³ μλ² ν¨μκ° <form>μ action νλ‘νΌν°μ μ λ¬λλ©΄, νΌμ μ μ§μ μΌλ‘ ν₯μλ©λλ€.
νΌμ΄ μ μΆλλ λμ λκΈ° μν 보μ¬μ£ΌκΈ° {/display-a-pending-state-during-form-submission/}
νΌμ΄ μ μΆλλ λμ λκΈ°Pending μνλ₯Ό 보μ¬μ£ΌκΈ° μν΄, <form>μ΄ λ λλ§λλ μ»΄ν¬λνΈ μμμ useFormStatus Hookμ νΈμΆν΄ λ°νλ pending νλ‘νΌν°λ₯Ό μ½μ μ μμ΅λλ€.
μ¬κΈ° νΌμ΄ μ μΆλκ³ μμμ λνλ΄κΈ° μν΄ pending νλ‘νΌν°λ₯Ό μ¬μ©νμμ΅λλ€.
import { useFormStatus } from "react-dom";
import { submitForm } from "./actions.js";
function Submit() {
const { pending } = useFormStatus();
return (
<button type="submit" disabled={pending}>
{pending ? "μ μΆμ€..." : "μ μΆ"}
</button>
);
}
function Form({ action }) {
return (
<form action={action}>
<Submit />
</form>
);
}
export default function App() {
return <Form action={submitForm} />;
}export async function submitForm(query) {
await new Promise((res) => setTimeout(res, 1000));
}useFormStatus Hookμ λν΄ λ μκ³ μΆλ€λ©΄, μ°Έκ³ λ¬Έμλ₯Ό νμΈνμΈμ.
useOptimistic Hookμ λ€νΈμν¬ μμ²κ³Ό κ°μ λ°±κ·ΈλΌμ΄λμ μμ
μ΄ λλκΈ° μ μ μ¬μ©μ μΈν°νμ΄μ€μ λκ΄μ μΌλ‘ μ
λ°μ΄νΈνλ λ°©λ²μ μ 곡ν©λλ€. νΌμ λ§₯λ½μμ μ΄ κΈ°μ μ μ±μ λμ± λ°μνμΌλ‘ λλΌκ² ν΄μ€λλ€. μ¬μ©μκ° νΌμ μ μΆνλ©΄ μΈν°νμ΄μ€λ μ¬μ©μκ° κΈ°λνλ κ²°κ³Όλ¬Όλ‘ μ¦μ μ
λ°μ΄νΈλ©λλ€.
μλ₯Ό λ€μ΄, μ¬μ©μκ° νΌμ λ©μμ§λ₯Ό μ
λ ₯νκ³ "μ μ‘" λ²νΌμ ν΄λ¦νλ©΄ useOptimistic Hookμ "μ μ‘μ€..." λΌλ²¨κ³Ό ν¨κ» λ©μμ§κ° μλ²μ 보λ΄μ§κΈ° μ μ 리μ€νΈμ μ¦μ 보μ
λλ€. μ΄λ¬ν 'λκ΄μ μΈ' μ κ·Ό λ°©μμ μλμ λ°μμ±μ΄ λ°μ΄λλ€λ μΈμμ μ€λλ€. κ·Έλ€μ νΌμ μ€μ λ‘ λ°±κ·ΈλΌμ΄λμ λ©μμ§ λ³΄λ΄κΈ°λ₯Ό μλν©λλ€. μλ²μ λ©μμ§κ° μ λμ°©νλ©΄, "μ μ‘μ€..." λΌλ²¨μ μ¬λΌμ§λλ€.
import { useOptimistic, useState, useRef } from "react";
import { deliverMessage } from "./actions.js";
function Thread({ messages, sendMessage }) {
const formRef = useRef();
async function formAction(formData) {
addOptimisticMessage(formData.get("message"));
formRef.current.reset();
await sendMessage(formData);
}
const [optimisticMessages, addOptimisticMessage] = useOptimistic(
messages,
(state, newMessage) => [
...state,
{
text: newMessage,
sending: true
}
]
);
return (
<>
{optimisticMessages.map((message, index) => (
<div key={index}>
{message.text}
{!!message.sending && <small> (μ μ‘μ€...)</small>}
</div>
))}
<form action={formAction} ref={formRef}>
<input type="text" name="message" placeholder="Hello!" />
<button type="submit">μ μ‘</button>
</form>
</>
);
}
export default function App() {
const [messages, setMessages] = useState([
{ text: "Hello there!", sending: false, key: 1 }
]);
async function sendMessage(formData) {
const sentMessage = await deliverMessage(formData.get("message"));
setMessages((messages) => [...messages, { text: sentMessage }]);
}
return <Thread messages={messages} sendMessage={sendMessage} />;
}export async function deliverMessage(message) {
await new Promise((res) => setTimeout(res, 1000));
return message;
}<form>μ action νλ‘νΌν°λ‘ μ λ¬λ μ΄λ€ ν¨μλ μ€λ₯λ₯Ό λμ§κΈ°λ ν©λλ€. μ΄λ° μ€λ₯λ₯Ό <form>μ μλ¬ λ°μ΄λ리λ₯Ό κ°μΈ ν΄κ²°ν μ μμ΅λλ€. λ§μ½ <form>μ action νλ‘νΌν°μμ νΈμΆλ ν¨μκ° μ€λ₯λ₯Ό λμ§λ€λ©΄ μλ¬ λ°μ΄λ리μ Fallbackμ΄ λ³΄μ΄κ² λ©λλ€.
import { ErrorBoundary } from "react-error-boundary";
export default function Search() {
function search() {
throw new Error("search error");
}
return (
<ErrorBoundary
fallback={<p>νΌ μ μΆ μ€μ μ€λ₯κ° λ°μνμ΅λλ€.</p>}
>
<form action={search}>
<input name="query" />
<button type="submit">κ²μ</button>
</form>
</ErrorBoundary>
);
}{
"dependencies": {
"react": "19.0.0-rc-3edc000d-20240926",
"react-dom": "19.0.0-rc-3edc000d-20240926",
"react-scripts": "^5.0.0",
"react-error-boundary": "4.0.3"
},
"main": "/index.js",
"devDependencies": {}
}μλ°μ€ν¬λ¦½νΈ μμ΄ νΌ μ μΆ μ€λ₯ 보μ¬μ£ΌκΈ° {/display-a-form-submission-error-without-javascript/}
μ μ§μ ν₯μμ μν΄ μλ°μ€ν¬λ¦½νΈ λ²λ€μ΄ λ‘λλκΈ° μ μ€λ₯ λ©μμ§λ₯Ό 보μ¬μ£ΌκΈ° μν΄ λ€μ μμλ€μ΄ μ§μΌμ ΈμΌ ν©λλ€.
<form>be rendered by a Client Component- the function passed to the
<form>'sactionprop be a Server Function - the
useActionStateHook be used to display the error message
useActionStateλ μλ² ν¨μμ μ΄κΈ° StateλΌλ λ κ°μ λ§€κ°λ³μλ₯Ό κ°μ§λλ€. useActionStateλ State λ³μμ μ‘μ
μ΄λΌλ λ κ°μ κ°μ λ°νν©λλ€. useActionStateλ₯Ό ν΅ν΄ λ°νλ μ‘μ
μ νΌμ action νλ‘νΌν°μ μ λ¬λ μ μμ΅λλ€. useActionStateλ₯Ό ν΅ν΄ λ°νλ μν λ³μλ μ€λ₯ λ©μμ§λ₯Ό 보μ¬μ£Όλ λ° μ¬μ©λ©λλ€. useActionStateμ μ λ¬λ μλ² ν¨μμμ λ°νλ κ°μ State λ³μλ₯Ό μ
λ°μ΄νΈνλ λ° μ¬μ©λ©λλ€.
import { useActionState } from "react";
import { signUpNewUser } from "./api";
export default function Page() {
async function signup(prevState, formData) {
"use server";
const email = formData.get("email");
try {
await signUpNewUser(email);
alert(`"${email}"μ λ±λ‘νμ΄μ`);
} catch (err) {
return err.toString();
}
}
const [message, signupAction] = useActionState(signup, null);
return (
<>
<h1>λ΄μ€λ ν°μ κ°μ
νμΈμ</h1>
<p>κ°μ μ΄λ©μΌλ‘ λ λ² κ°μ
νμ¬ μ€λ₯λ₯Ό νμΈνμΈμ.</p>
<form action={signupAction} id="signup-form">
<label htmlFor="email">μ΄λ©μΌ: </label>
<input name="email" id="email" placeholder="react@example.com" />
<button>κ°μ
νκΈ°</button>
{!!message && <p>{message}</p>}
</form>
</>
);
}let emails = [];
export async function signUpNewUser(newEmail) {
if (emails.includes(newEmail)) {
throw new Error("μ΄ μ΄λ©μΌ μ£Όμλ μ΄λ―Έ λ±λ‘λμμ΅λλ€.");
}
emails.push(newEmail);
}useActionState λ¬Έμλ₯Ό ν΅ν΄ νΌ μμ
μμ μνλ₯Ό μ
λ°μ΄νΈνλ λ°©λ²μ λν΄ μμΈν μμ보μΈμ.
μ¬μ©μκ° λλ₯Έ λ²νΌμ λ°λΌ μ¬λ¬ μ μΆ μμ
μ μ²λ¦¬νλλ‘ νΌμ μ€κ³ν μ μμ΅λλ€. νΌ λ΄λΆμ κ° λ²νΌμ formAction νλ‘νΌν°λ₯Ό μ€μ νμ¬ κ³ μ ν λμ λλ λμκ³Ό μ°κ²°ν μ μμ΅λλ€.
μ¬μ©μκ° νΉμ λ²νΌμ ν΄λ¦νλ©΄ νΌμ΄ μ μΆλκ³ ν΄λΉ λ²νΌμ μμ± λ° λμμΌλ‘ μ μλ ν΄λΉ λμμ΄ μ€νλ©λλ€. μλ₯Ό λ€μ΄, νΌμ κΈ°λ³Έμ μΌλ‘ κ²ν λ₯Ό μν΄ λ¬Έμλ₯Ό μ μΆνμ§λ§ formActionμ΄ μ€μ λ λ³λμ λ²νΌμ΄ μμ΄ λ¬Έμλ₯Ό μ΄μμΌλ‘ μ μ₯ν μ μμ΅λλ€.
export default function Search() {
function publish(formData) {
const content = formData.get("content");
const button = formData.get("button");
alert(`'${button}' λ²νΌμΌλ‘ '${content}'κ° λ°νλμμ΅λλ€.`);
}
function save(formData) {
const content = formData.get("content");
alert(`'${content}' μ΄μμ΄ μ μ₯λμμ΅λλ€!`);
}
return (
<form action={publish}>
<textarea name="content" rows={4} cols={40} />
<br />
<button type="submit" name="button" value="submit">λ°ν</button>
<button formAction={save}>μ΄μ μ μ₯</button>
</form>
);
}