Skip to content

Commit ddb9805

Browse files
committed
docs: update useActionState.md
1 parent 0cdb1fc commit ddb9805

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

src/content/reference/react/useActionState.md

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: useActionState
44

55
<Intro>
66

7-
`useActionState`는 폼 액션의 결과를 기반으로 state를 업데이트할 수 있도록 제공하는 Hook 입니다.
7+
`useActionState`는 폼 액션의 결과를 기반으로 state를 업데이트할 수 있도록 제공하는 Hook입니다.
88

99
```js
1010
const [state, formAction, isPending] = useActionState(fn, initialState, permalink?);
@@ -14,11 +14,10 @@ const [state, formAction, isPending] = useActionState(fn, initialState, permalin
1414
1515
<Note>
1616
17-
In earlier React Canary versions, this API was part of React DOM and called `useFormState`.
17+
이전 React Canary 버전에서는 이 API가 React DOM에 포함되어 있었고, `useFormState`라고 불렸습니다.
1818
1919
</Note>
2020
21-
2221
<InlineToc />
2322
2423
---
@@ -29,7 +28,7 @@ In earlier React Canary versions, this API was part of React DOM and called `use
2928
3029
{/* TODO T164397693: link to actions documentation once it exists */}
3130
32-
컴포넌트 최상위 레벨에서 `useActionState`를 호출하여 [폼 액션이 실행될 때](/reference/react-dom/components/form) 업데이트되는 컴포넌트 state를 생성합니다. `useActionState`는 기존의 폼 액션 함수와 초기 state를 전달받고, 폼에서 사용할 새로운 액션을 반환합니다. 이와 함께 최신 폼 state와 액션이 여전히 진행(Pending) 중인지 여부도 반환합니다. 최신 폼 State는 제공된 함수에도 전달됩니다.
31+
컴포넌트 함수(또는 최상위 레벨)에서 `useActionState`를 호출하여 [폼 액션이 실행될 때](/reference/react-dom/components/form) 업데이트되는 컴포넌트 state를 생성합니다. `useActionState`는 기존의 폼 액션 함수와 초기 state를 전달받고, 폼에서 사용할 새로운 액션을 반환합니다. 또한 최신 폼 state와 액션이 진행 중인지 여부(`isPending`)도 반환합니다. 이때 최신 폼 state는 `useActionState`에 전달한 함수에도 함께 전달됩니다.
3332
3433
```js
3534
import { useActionState } from "react";
@@ -45,50 +44,50 @@ function StatefulForm({}) {
4544
{state}
4645
<button formAction={formAction}>Increment</button>
4746
</form>
48-
)
47+
);
4948
}
5049
```
5150
52-
폼 state는 폼을 마지막으로 제출했을 때 액션에서 반환되는 값입니다. 폼이 제출되기 전이라면 전달한 초기 state와 같습니다.
51+
폼 state는 폼을 마지막으로 제출했을 때 액션에서 반환되는 값입니다. 아직 폼을 제출하지 않았다면, `initialState`로 설정됩니다.
5352
54-
Server Action과 함께 사용하는 경우, `useActionState`사용하여 hydration이 완료되기 전에도 폼 제출에 대한 서버의 응답을 보여줄 수 있습니다.
53+
서버 함수(Server Function)와 함께 사용하는 경우, `useActionState`통해 하이드레이션(hydration)이 끝나기 전에도 폼 제출에 대한 서버 응답을 표시할 수 있습니다.
5554
5655
If used with a Server Function, `useActionState` allows the server's response from submitting the form to be shown even before hydration has completed. {/*TODO*/}
5756
5857
[아래에서 더 많은 예시를 확인해 보세요.](#usage)
5958
6059
#### 매개변수 {/*parameters*/}
6160
62-
* `fn`: 폼이 제출되거나 버튼을 눌렀을 때 호출될 함수입니다. 함수가 실행될 때, 첫 번째 인수로 폼의 이전 state를 전달합니다. state는 초기에 전달한 `initialState`이고, 이후에는 이전 실행의 반환값입니다. 그 후 일반적으로액션에 전달하는 인수들이 이어집니다.
63-
* `initialState`: 초기 state로 설정하고자 하는 값으로, 직렬화 할 수 있는 값일 수 있습니다. 액션이 처음 호출된 후에는 이 인수를 무시합니다.
64-
* **optional** `permalink`: 이 폼이 수정하는 고유의 URL이 포함된 문자열입니다. 점진적인 향상과 함께 동적 콘텐츠(예: 피드)가 있는 페이지에서 사용합니다. `fn`이 [서버 액션](/reference/react/use-server)이고 폼이 자바스크립트 번들이 로드되기 전에 제출된다면, 브라우저는 현재의 페이지 URL이 아닌 명시된 `permalink` URL로 이동합니다. React가 상태를 전달하는 방법을 알 수 있도록 동일한 폼 컴포넌트가 대상 페이지에 렌더링 되어야 합니다(동일한 액션 `fn``permalink` 포함). 폼이 hydrated하면 이 매개변수는 아무런 영향을 미치지 않습니다.
61+
* `fn`: 폼이 제출되거나 버튼이 눌렸을 때 호출될 함수입니다. 함수가 호출되면 첫 번째 인수로 폼의 이전 state가 전달됩니다(처음에는 `initialState`, 그 뒤로는 액션의 마지막 반환값). 그 뒤 일반적인액션과 동일하게 인수들이 이어집니다.
62+
* `initialState`: 초기 state로 사용될 직렬화 가능한 값입니다. 액션이 처음 호출된 뒤에는 더 이상 이 값이 사용되지 않습니다.
63+
* **optional** `permalink`: 점진적 향상(progressive enhancement) 기법을 사용하는 동적 콘텐츠(예: 피드) 페이지에서 유용한 옵션입니다. `fn`이 [서버 함수(Server Function)](/reference/rsc/server-functions)이고, 자바스크립트 번들이 로드되기 전에 폼이 제출된다면 브라우저는 현재 페이지가 아니라 이 `permalink` URL로 이동합니다. React가 상태를 올바르게 이어 받기 위해, 대상 페이지에서도 동일한 폼 컴포넌트를 렌더링해야 합니다(`fn``permalink`가 동일해야 함). 폼이 하이드레이션된 뒤에는 이 매개변수는 더 이상 영향을 주지 않습니다.
6564
6665
* `fn`: The function to be called when the form is submitted or button pressed. When the function is called, it will receive the previous state of the form (initially the `initialState` that you pass, subsequently its previous return value) as its initial argument, followed by the arguments that a form action normally receives.
6766
* `initialState`: The value you want the state to be initially. It can be any serializable value. This argument is ignored after the action is first invoked.
68-
* **optional** `permalink`: A string containing the unique page URL that this form modifies. For use on pages with dynamic content (eg: feeds) in conjunction with progressive enhancement: if `fn` is a [server function](/reference/rsc/server-functions) and the form is submitted before the JavaScript bundle loads, the browser will navigate to the specified permalink URL, rather than the current page's URL. Ensure that the same form component is rendered on the destination page (including the same action `fn` and `permalink`) so that React knows how to pass the state through. Once the form has been hydrated, this parameter has no effect. {/*TODO*/}
67+
* **optional** `permalink`: A string containing the unique page URL that this form modifies. For use on pages with dynamic content (e.g. feeds) in conjunction with progressive enhancement: if `fn` is a [server function](/reference/rsc/server-functions) and the form is submitted before the JavaScript bundle loads, the browser will navigate to the specified permalink URL, rather than the current page's URL. Ensure that the same form component is rendered on the destination page (including the same action `fn` and `permalink`) so that React knows how to pass the state through. Once the form has been hydrated, this parameter has no effect. {/*TODO*/}
6968
7069
{/* TODO T164397693: link to serializable values docs once it exists */}
7170
7271
#### 반환값 {/*returns*/}
7372
74-
`useActionState`는 다음 3가지 값들이 포함된 배열을 반환합니다.
73+
`useActionState`는 다음 세 가지 값을 담은 배열을 반환합니다.
7574
76-
1. 현재 state입니다. 첫 번째 렌더링에서는 전달한 `initialState`일치합니다. 액션이 실행된 이후에는 액션에서 반환한 값과 일치합니다.
77-
2. `form` 컴포넌트의 `action` prop에 전달하거나 폼 내부 `button` 컴포넌트의 `formAction` prop에 전달할 수 있는 새로운 액션입니다.
78-
3. 대기 중인 전환(Pending Transition)이 있는지 여부를 알려주는 `isPending` 플래그입니다.
75+
1. 현재 state입니다. 첫 렌더링 시에는 `initialState`일치하며, 액션이 실행된 후에는 액션이 반환한 값과 일치합니다.
76+
2. `<form>` 컴포넌트의 `action` prop이나 폼 내부 `<button>` 컴포넌트의 `formAction` prop에 전달할 수 있는 액션입니다.
77+
3. 폼 액션이 진행 중인지 여부를 알려주는 `isPending` 플래그입니다.
7978
8079
#### 주의 사항 {/*caveats*/}
8180
82-
* React 서버 컴포넌트를 지원하는 프레임워크와 함께 사용할 때, `useActionState`는 클라이언트에서 자바스크립트가 실행되기 이전에도 폼을 상호작용하도록 만들 수 있습니다. 서버 컴포넌트를 사용하지 않는다면 컴포넌트 지역 state와 동일합니다.
83-
* `useActionState`전달한 함수는 첫 번째 인수로 이전 혹은 초기 state를 추가로 받습니다. 이를 통해 `useActionState`를 사용하지 않고 직접 폼 액션을 사용했을 경우와는 다른 시그니처를 가지게 됩니다.
81+
* React 서버 컴포넌트를 지원하는 프레임워크에서 `useActionState`를 사용하면, 클라이언트 자바스크립트 실행 전에도 폼과 상호작용할 수 있습니다. 만약 서버 컴포넌트를 사용하지 않는다면, 이는 단순히 컴포넌트 로컬 state와 동일하게 동작합니다.
82+
* `useActionState`전달된 함수는 첫 번째 인수로 이전 또는 초기 state를 추가로 받습니다. 즉, 직접 폼 액션을 사용했을 때와 비교해 함수의 시그니처가 달라질 수 있습니다.
8483
8584
---
8685
8786
## 사용법 {/*usage*/}
8887
8988
### 폼 액션에서 반환된 정보 사용하기 {/*using-information-returned-by-a-form-action*/}
9089
91-
컴포넌트의 최상위 레벨에서 `useActionState`를 호출하면 폼이 마지막으로 제출된 시점에 액션이 반환한 값에 접근할 수 있습니다.
90+
컴포넌트의 최상위 레벨에서 `useActionState`를 호출하면, 폼이 마지막으로 제출되었을 때 액션이 반환한 값에 접근할 수 있습니다.
9291
9392
```js [[1, 5, "state"], [2, 5, "formAction"], [3, 5, "action"], [4, 5, "null"], [2, 8, "formAction"]]
9493
import { useActionState } from 'react';
@@ -105,15 +104,15 @@ function MyComponent() {
105104
}
106105
```
107106
108-
`useActionState`는 다음 3가지 항목들이 포함된 배열을 반환합니다.
107+
`useActionState`가 반환하는 배열은 다음과 같은 요소를 갖습니다:
109108
110109
1. 폼의 <CodeStep step={1}>현재 state</CodeStep>입니다. 처음에는 전달한 <CodeStep step={4}>초기 state</CodeStep>로 설정되며, 폼이 제출된 후에는 전달한 <CodeStep step={3}>액션</CodeStep>의 반환값으로 설정됩니다.
111110
2. `<form>``action` prop에 전달할 <CodeStep step={2}>새로운 액션</CodeStep>입니다.
112111
3. 액션이 처리되는 동안 사용할 수 있는 <CodeStep step={1}>대기(Pending) state</CodeStep>입니다.
113112
114-
폼을 제출하면 전달한 <CodeStep step={3}>액션</CodeStep> 함수가 호출됩니다. 액션의 반환값은 폼의 새로운 <CodeStep step={1}>현재 state</CodeStep>가 됩니다.
113+
폼 제출 시, <CodeStep step={3}>액션</CodeStep> 함수가 호출되고 그 반환값이 폼의 새로운 <CodeStep step={1}>현재 state</CodeStep>가 됩니다.
115114
116-
전달한 <CodeStep step={3}>액션</CodeStep>은 또한 폼의 <CodeStep step={1}>현재 state</CodeStep>를 새로운 첫 번째 인수로 받게 됩니다. 폼이 처음 제출되면 제공한 <CodeStep step={4}>초기 state</CodeStep>이며, 이후 제출에서는 액션이 마지막으로 호출된 시점의 반환값이 됩니다. 나머지 인수는 `useActionState`를 사용하지 않았을 때와 동일합니다.
115+
또한 <CodeStep step={3}>액션</CodeStep> 함수는 새롭게 추가된 첫 번째 인수로 폼의 <CodeStep step={1}>현재 state</CodeStep>를 받습니다. (처음엔 <CodeStep step={4}>초기 state</CodeStep>, 그 후에는 직전 반환값) 나머지 인수들은 일반 폼 액션 호출과 동일합니다.
117116
118117
```js [[3, 1, "action"], [1, 1, "currentState"]]
119118
function action(currentState, formData) {
@@ -126,14 +125,14 @@ function action(currentState, formData) {
126125
127126
#### 오류 표시하기 {/*display-form-errors*/}
128127
129-
Server Action에서 반환한 오류 메시지나 토스트와 같은 메시지를 표시하려면 해당 액션을 `useActionState` 호출로 감싸세요.
128+
서버 함수(Server Function)에서 반환된 오류 메시지나 토스트 메시지를 표시하려면, 해당 액션을 `useActionState`로 감싸 주세요.
130129
131130
To display messages such as an error message or toast that's returned by a Server Function, wrap the action in a call to `useActionState`.
132131
133132
<Sandpack>
134133
135134
```js src/App.js
136-
import { useActionState, useState } from "react";
135+
import { useActionState } from "react";
137136
import { addToCart } from "./actions.js";
138137

139138
function AddToCartForm({itemID, itemTitle}) {
@@ -154,7 +153,7 @@ export default function App() {
154153
<AddToCartForm itemID="1" itemTitle="JavaScript: The Definitive Guide" />
155154
<AddToCartForm itemID="2" itemTitle="JavaScript: The Good Parts" />
156155
</>
157-
)
156+
);
158157
}
159158
```
160159
@@ -179,7 +178,7 @@ export async function addToCart(prevState, queryData) {
179178
form {
180179
border: solid 1px black;
181180
margin-bottom: 24px;
182-
padding: 12px
181+
padding: 12px;
183182
}
184183

185184
form button {
@@ -192,7 +191,7 @@ form button {
192191
193192
#### 폼 제출 후 구조화된 정보 표시하기 {/*display-structured-information-after-submitting-a-form*/}
194193
195-
Server Action의 반환값으로 어떠한 직렬화 가능한 값이든 될 수 있습니다. 예를 들어, 액션이 성공적인지를 나타내는 불리언, 오류 메시지, 또는 업데이트된 정보를 포함하는 객체일 수 있습니다.
194+
서버 함수(Server Function)의 반환값은 직렬화 가능한 어떤 값이든 가능합니다. 예를 들어, 액션 성공 여부를 나타내는 불리언, 오류 메시지, 업데이트된 객체 등 다양하게 활용할 수 있습니다.
196195
197196
The return value from a Server Function can be any serializable value. For example, it could be an object that includes a boolean indicating whether the action was successful, an error message, or updated information. {/*TODO*/}
198197
@@ -229,7 +228,7 @@ export default function App() {
229228
<AddToCartForm itemID="1" itemTitle="JavaScript: The Definitive Guide" />
230229
<AddToCartForm itemID="2" itemTitle="JavaScript: The Good Parts" />
231230
</>
232-
)
231+
);
233232
}
234233
```
235234
@@ -256,7 +255,7 @@ export async function addToCart(prevState, queryData) {
256255
form {
257256
border: solid 1px black;
258257
margin-bottom: 24px;
259-
padding: 12px
258+
padding: 12px;
260259
}
261260

262261
form button {
@@ -273,7 +272,7 @@ form button {
273272
274273
### 액션이 더 이상 제출된 폼 데이터를 읽을 수 없습니다 {/*my-action-can-no-longer-read-the-submitted-form-data*/}
275274
276-
액션을 `useActionState`로 감싸면 *첫 번째 인수*를 추가로 전달받습니다. 따라서 제출된데이터는 보통의 경우처럼 첫 번째로 전달되는 대신 *두 번째* 인수가 됩니다. 새롭게 추가된 첫 번째 인수는 폼의 현재 state입니다.
275+
액션을 `useActionState`로 감싸면 *첫 번째 인수*로 “이전(또는 현재) state”가 추가됩니다. 따라서 일반적인액션과 달리, 제출된 폼 데이터는 *두 번째 인수*에서 확인해야 합니다.
277276
278277
```js
279278
function action(currentState, formData) {

0 commit comments

Comments
 (0)