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
+49-45
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`.
{/* 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`即可创建一个随 [表单动作被调用](/reference/react-dom/components/form) 而更新的 state。在调用`useFormState`时在参数中传入现有的表单动作函数以及一个初始状态,它就会返回一个新的 action 函数和一个 form state 以供在 form 中使用。这个新的 form state 也会作为参数传入提供的表单动作函数。
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
+
form state 是一个只在表单被提交触发 action 后才会被更新的值。如果该表单没有被提交,该值会保持传入的初始值不变。
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.
54
+
如果配合 Server Actions 一起使用,`useFormState`允许与表单交互的服务器的返回值在 hydration 完成前显示。
55
55
56
-
[See more examples below.](#usage)
56
+
[请参阅下方更多示例](#usage)。
57
57
58
-
#### Parameters {/*parameters*/}
58
+
#### 参数 {/*parameters*/}
59
59
60
-
*`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.
{/* 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`返回一个包含两个值的数组:
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 服务器组件的框架中使用该功能时,`useFormState`允许表单在服务器渲染阶段时获得部分交互性。当不使用服务器组件时,它的特性与本地 state 相同。
75
+
*与直接通过表单动作调用的函数不同,传入 `useFormState`的函数被调用时,会多传入一个代表 state 的上一个值或初始值的参数作为该函数的第一个参数。
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`返回一个包含两个值的数组:
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.
193
+
Server Actions 的返回值可以为任意可序列化的值。例如,可以返回一个实例,该实例携带一个 boolean 类型的属性表示操作是否成功,同时附带错误信息或更新消息。
194
194
195
195
<Sandpack>
196
196
@@ -205,15 +205,15 @@ function AddToCartForm({itemID, itemTitle}) {
@@ -243,7 +243,7 @@ export async function addToCart(prevState, queryData) {
243
243
} else {
244
244
return {
245
245
success:false,
246
-
message:"The item is sold out.",
246
+
message:"商品已售罄",
247
247
};
248
248
}
249
249
}
@@ -278,14 +278,18 @@ form button {
278
278
279
279
</Recipes>
280
280
281
-
## Troubleshooting {/*troubleshooting*/}
281
+
## 疑难解答 {/*troubleshooting*/}
282
282
283
-
### My action can no longer read the submitted form data {/*my-action-can-no-longer-read-the-submitted-form-data*/}
283
+
### 我的 action 无法再获取提交的 form data 了 {/*my-action-can-no-longer-read-the-submitted-form-data*/}
284
284
285
-
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.
285
+
当使用 `useFormState` 包裹 action 时,第一个参数变为了 form 的当前 state,提交的表单数据被顺移到了第二个参数中,与直接使用表单动作是不同的。
0 commit comments