You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/reference/react-dom/hooks/useFormState.md
+33-33
Original file line number
Diff line number
Diff line change
@@ -5,13 +5,13 @@ canary: true
5
5
6
6
<Canary>
7
7
8
-
The `useFormState`Hook is currently only available in React's Canary and experimental channels. Learn more about [release channels here](/community/versioning-policy#all-release-channels). In addition, you need to use a framework that supports [React Server Components](/reference/react/use-client)to get the full benefit of `useFormState`.
8
+
`useFormState`フックは、現在 React の Canary および experimental チャンネルでのみ利用可能です。[リリースチャンネルについてはこちらをご覧ください](/community/versioning-policy#all-release-channels)。また、`useFormState` の利点をフルに活かすためには、[React Server Components](/reference/react/use-client)をサポートするフレームワークを使用する必要があります。
9
9
10
10
</Canary>
11
11
12
12
<Intro>
13
13
14
-
`useFormState`is a Hook that allows you to update state based on the result of a form action.
14
+
`useFormState`は、フォームアクションの結果に基づいて state を更新するためのフックです。
{/* TODO T164397693: link to actions documentation once it exists */}
31
31
32
-
Call`useFormState`at the top level of your component to create component state that is updated [when a form action is invoked](/reference/react-dom/components/form). You pass`useFormState`an existing form action function as well as an initial state, and it returns a new action that you use in your form, along with the latest form state. The latest form state is also passed to the function that you provided.
32
+
コンポーネントのトップレベルで`useFormState`を呼び出してコンポーネントの state を作成し、[フォームアクションが呼び出されたとき](/reference/react-dom/components/form)に更新されるようにします。既存のフォームアクション関数と初期 state を`useFormState`に渡し、フォームで使用する新しいアクションと最新のフォーム state が返されます。あなたが渡した関数にも、最新のフォーム state が渡されるようになります。
33
33
34
34
```js
35
35
import { useFormState } from"react-dom";
@@ -49,38 +49,38 @@ function StatefulForm({}) {
49
49
}
50
50
```
51
51
52
-
The form state is the value returned by the action when the form was last submitted. If the form has not yet been submitted, it is the initial state that you pass.
52
+
フォーム state とは、フォームが最後に送信されたときにアクションによって返される値です。フォームがまだ送信されていない場合は、渡された初期 state が使われます。
53
53
54
-
If used with a Server Action, `useFormState`allows the server's response from submitting the form to be shown even before hydration has completed.
*`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.
61
-
*`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.
*`initialState`: state の初期値として使いたい値。シリアライズ可能な任意の値です。この引数はアクションが一度呼び出された後は無視されます。
62
62
63
63
{/* TODO T164397693: link to serializable values docs once it exists */}
64
64
65
-
#### Returns {/*returns*/}
65
+
#### 返り値 {/*returns*/}
66
66
67
-
`useFormState`returns an array with exactly two values:
67
+
`useFormState`は 2 つの値を含む配列を返します。
68
68
69
-
1.The current state. During the first render, it will match the `initialState`you have passed. After the action is invoked, it will match the value returned by the action.
70
-
2.A new action that you can pass as the `action`prop to your `form` component or `formAction`prop to any `button` component within the form.
*When used with a framework that supports React Server Components, `useFormState`lets you make forms interactive before JavaScript has executed on the client. When used without Server Components, it is equivalent to component local state.
75
-
*The function passed to `useFormState`receives an extra argument, the previous or initial state, as its first argument. This makes its signature different than if it were used directly as a form action without using `useFormState`.
74
+
* React Server Components をサポートするフレームワークで使用する場合、`useFormState`はクライアント上で JavaScript が実行される前にフォームを操作可能にできます。Server Components なしで使用する場合、コンポーネントのローカル state と同様のものになります。
75
+
*`useFormState`に渡される関数は、追加の 1 番目の引数として、前回 state ないし初期 state を受け取ります。従って `useFormState` を使用せずに直接フォームアクションとして使用する場合とは異なるシグネチャになります。
76
76
77
77
---
78
78
79
-
## Usage {/*usage*/}
79
+
## 使用法 {/*usage*/}
80
80
81
-
### Using information returned by a form action {/*using-information-returned-by-a-form-action*/}
`useFormState`returns an array with exactly two items:
100
+
`useFormState`は、2 つの項目を含む配列を返します。
101
101
102
-
1.The <CodeStepstep={1}>current state</CodeStep> of the form, which is initially set to the <CodeStepstep={4}>initial state</CodeStep> you provided, and after the form is submitted is set to the return value of the <CodeStepstep={3}>action</CodeStep> you provided.
103
-
2.A <CodeStepstep={2}>new action</CodeStep> that you pass to `<form>` as its `action` prop.
When the form is submitted, the <CodeStepstep={3}>action</CodeStep> function that you provided will be called. Its return value will become the new <CodeStepstep={1}>current state</CodeStep> of the form.
The <CodeStepstep={3}>action</CodeStep> that you provide will also receive a new first argument, namely the <CodeStepstep={1}>current state</CodeStep> of the form. The first time the form is submitted, this will be the <CodeStepstep={4}>initial state</CodeStep> you provided, while with subsequent submissions, it will be the return value from the last time the action was called. The rest of the arguments are the same as if `useFormState`had not been used
The return value from a Server Action 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.
When you wrap an action with `useFormState`, it gets an extra argument *as its first argument*. The submitted form data is therefore its *second* argument instead of its first as it would usually be. The new first argument that gets added is the current state of the form.
0 commit comments