From 42ca996138ba45775d0ba93128b3f2998d0828bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xuan=20Huang=20=28=E9=BB=84=E7=8E=84=29?= Date: Tue, 7 Nov 2023 19:56:44 +0800 Subject: [PATCH] Captailize Server Action (#6417) Summary of changes: "Server Action", like "Effect", is a React-specific notion that would be benefited from captailization to be distinguished from its genertic meaning. It seems like [Next doc](https://nextjs.org/docs/app/api-reference/functions/server-actions) has also adopted such connventions and we should probably do the same. Co-authored-by: xuan.huang --- .../reference/react-dom/components/form.md | 6 +-- .../reference/react-dom/hooks/useFormState.md | 6 +-- src/content/reference/react/use-client.md | 4 +- src/content/reference/react/use-server.md | 48 +++++++++---------- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/content/reference/react-dom/components/form.md b/src/content/reference/react-dom/components/form.md index dfffc74f561..25d1ba4e9f1 100644 --- a/src/content/reference/react-dom/components/form.md +++ b/src/content/reference/react-dom/components/form.md @@ -93,11 +93,11 @@ export default function Search() { ### Handle form submission with a Server Action {/*handle-form-submission-with-a-server-action*/} -Render a `
` with an input and submit button. Pass a server action (a function marked with [`'use server'`](/reference/react/use-server)) to the `action` prop of form to run the function when the form is submitted. +Render a `` with an input and submit button. Pass a Server Action (a function marked with [`'use server'`](/reference/react/use-server)) to the `action` prop of form to run the function when the form is submitted. -Passing a server action to `` allow users to submit forms without JavaScript enabled or before the code has loaded. This is beneficial to users who have a slow connection, device, or have JavaScript disabled and is similar to the way forms work when a URL is passed to the `action` prop. +Passing a Server Action to `` allow users to submit forms without JavaScript enabled or before the code has loaded. This is beneficial to users who have a slow connection, device, or have JavaScript disabled and is similar to the way forms work when a URL is passed to the `action` prop. -You can use hidden form fields to provide data to the ``'s action. The server action will be called with the hidden form field data as an instance of [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData). +You can use hidden form fields to provide data to the ``'s action. The Server Action will be called with the hidden form field data as an instance of [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData). ```jsx import { updateCart } from './lib.js'; diff --git a/src/content/reference/react-dom/hooks/useFormState.md b/src/content/reference/react-dom/hooks/useFormState.md index 53c73ae3874..b2808079c56 100644 --- a/src/content/reference/react-dom/hooks/useFormState.md +++ b/src/content/reference/react-dom/hooks/useFormState.md @@ -51,7 +51,7 @@ function StatefulForm({}) { 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. -If used with a server action, `useFormState` allows the server's response from submitting the form to be shown even before hydration has completed. +If used with a Server Action, `useFormState` allows the server's response from submitting the form to be shown even before hydration has completed. [See more examples below.](#usage) @@ -117,7 +117,7 @@ function action(currentState, formData) { #### Display form errors {/*display-form-errors*/} -To display messages such as an error message or toast that's returned by a server action, wrap the action in a call to `useFormState`. +To display messages such as an error message or toast that's returned by a Server Action, wrap the action in a call to `useFormState`. @@ -190,7 +190,7 @@ form button { #### Display structured information after submitting a form {/*display-structured-information-after-submitting-a-form*/} -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. +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. diff --git a/src/content/reference/react/use-client.md b/src/content/reference/react/use-client.md index f4b90d2883f..be8bd0089ae 100644 --- a/src/content/reference/react/use-client.md +++ b/src/content/reference/react/use-client.md @@ -269,7 +269,7 @@ Serializable props include: * [TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) and [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) * [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) * Plain [objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object): those created with [object initializers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer), with serializable properties -* Functions that are [server actions](/reference/react/use-server) +* Functions that are [Server Actions](/reference/react/use-server) * Client or Server Component elements (JSX) * [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) @@ -376,4 +376,4 @@ These libraries may rely on component Hooks or client APIs. Third-party componen If these libraries have been updated to be compatible with React Server Components, then they will already include `'use client'` markers of their own, allowing you to use them directly from your Server Components. If a library hasn't been updated, or if a component needs props like event handlers that can only be specified on the client, you may need to add your own Client Component file in between the third-party Client Component and your Server Component where you'd like to use it. -[TODO]: <> (Troubleshooting - need use-cases) \ No newline at end of file +[TODO]: <> (Troubleshooting - need use-cases) diff --git a/src/content/reference/react/use-server.md b/src/content/reference/react/use-server.md index 69f5e104467..938efa90355 100644 --- a/src/content/reference/react/use-server.md +++ b/src/content/reference/react/use-server.md @@ -25,7 +25,7 @@ canary: true ### `'use server'` {/*use-server*/} -Add `'use server'` at the top of an async function body to mark the function as callable by the client. We call these functions _server actions_. +Add `'use server'` at the top of an async function body to mark the function as callable by the client. We call these functions _Server Actions_. ```js {2} async function addToCart(data) { @@ -34,28 +34,28 @@ async function addToCart(data) { } ``` -When calling a server action on the client, it will make a network request to the server that includes a serialized copy of any arguments passed. If the server action returns a value, that value will be serialized and returned to the client. +When calling a Server Action on the client, it will make a network request to the server that includes a serialized copy of any arguments passed. If the Server Action returns a value, that value will be serialized and returned to the client. -Instead of individually marking functions with `'use server'`, you can add the directive to the top of a file to mark all exports within that file as server actions that can be used anywhere, including imported in client code. +Instead of individually marking functions with `'use server'`, you can add the directive to the top of a file to mark all exports within that file as Server Actions that can be used anywhere, including imported in client code. #### Caveats {/*caveats*/} * `'use server'` must be at the very beginning of their function or module; above any other code including imports (comments above directives are OK). They must be written with single or double quotes, not backticks. -* `'use server'` can only be used in server-side files. The resulting server actions can be passed to Client Components through props. See supported [types for serialization](#serializable-parameters-and-return-values). -* To import a server action from [client code](/reference/react/use-client), the directive must be used on a module level. +* `'use server'` can only be used in server-side files. The resulting Server Actions can be passed to Client Components through props. See supported [types for serialization](#serializable-parameters-and-return-values). +* To import a Server Action from [client code](/reference/react/use-client), the directive must be used on a module level. * Because the underlying network calls are always asynchronous, `'use server'` can only be used on async functions. -* Always treat arguments to server actions as untrusted input and authorize any mutations. See [security considerations](#security). -* Server actions should be called in a [transition](/reference/react/useTransition). Server actions passed to [``](/reference/react-dom/components/form#props) or [`formAction`](/reference/react-dom/components/input#props) will automatically be called in a transition. -* Server actions are designed for mutations that update server-side state; they are not recommended for data fetching. Accordingly, frameworks implementing server actions typically process one action at a time and do not have a way to cache the return value. +* Always treat arguments to Server Actions as untrusted input and authorize any mutations. See [security considerations](#security). +* Server Actions should be called in a [transition](/reference/react/useTransition). Server Actions passed to [``](/reference/react-dom/components/form#props) or [`formAction`](/reference/react-dom/components/input#props) will automatically be called in a transition. +* Server Actions are designed for mutations that update server-side state; they are not recommended for data fetching. Accordingly, frameworks implementing Server Actions typically process one action at a time and do not have a way to cache the return value. ### Security considerations {/*security*/} -Arguments to server actions are fully client-controlled. For security, always treat them as untrusted input, and make sure to validate and escape arguments as appropriate. +Arguments to Server Actions are fully client-controlled. For security, always treat them as untrusted input, and make sure to validate and escape arguments as appropriate. -In any server action, make sure to validate that the logged-in user is allowed to perform that action. +In any Server Action, make sure to validate that the logged-in user is allowed to perform that action. -To prevent sending sensitive data from a server action, there are experimental taint APIs to prevent unique values and objects from being passed to client code. +To prevent sending sensitive data from a Server Action, there are experimental taint APIs to prevent unique values and objects from being passed to client code. See [experimental_taintUniqueValue](/reference/react/experimental_taintUniqueValue) and [experimental_taintObjectReference](/reference/react/experimental_taintObjectReference). @@ -63,9 +63,9 @@ See [experimental_taintUniqueValue](/reference/react/experimental_taintUniqueVal ### Serializable arguments and return values {/*serializable-parameters-and-return-values*/} -As client code calls the server action over the network, any arguments passed will need to be serializable. +As client code calls the Server Action over the network, any arguments passed will need to be serializable. -Here are supported types for server action arguments: +Here are supported types for Server Action arguments: * Primitives * [string](https://developer.mozilla.org/en-US/docs/Glossary/String) @@ -84,12 +84,12 @@ Here are supported types for server action arguments: * [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) * [FormData](https://developer.mozilla.org/en-US/docs/Web/API/FormData) instances * Plain [objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object): those created with [object initializers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer), with serializable properties -* Functions that are server actions +* Functions that are Server Actions * [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) Notably, these are not supported: * React elements, or [JSX](https://react.dev/learn/writing-markup-with-jsx) -* Functions, including component functions or any other function that is not a server action +* Functions, including component functions or any other function that is not a Server Action * [Classes](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Classes_in_JavaScript) * Objects that are instances of any class (other than the built-ins mentioned) or objects with [a null prototype](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#null-prototype_objects) * Symbols not registered globally, ex. `Symbol('my new symbol')` @@ -100,9 +100,9 @@ Supported serializable return values are the same as [serializable props](/refer ## Usage {/*usage*/} -### Server actions in forms {/*server-actions-in-forms*/} +### Server Actions in forms {/*server-actions-in-forms*/} -The most common use case of server actions will be calling server functions that mutate data. On the browser, the [HTML form element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form) is the traditional approach for a user to submit a mutation. With React Server Components, React introduces first-class support for server actions in [forms](/reference/react-dom/components/form). +The most common use case of Server Actions will be calling server functions that mutate data. On the browser, the [HTML form element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form) is the traditional approach for a user to submit a mutation. With React Server Components, React introduces first-class support for Server Actions in [forms](/reference/react-dom/components/form). Here is a form that allows a user to request a username. @@ -123,15 +123,15 @@ export default App() { } ``` -In this example `requestUsername` is a server action passed to a ``. When a user submits this form, there is a network request to the server function `requestUsername`. When calling a server action in a form, React will supply the form's [FormData](https://developer.mozilla.org/en-US/docs/Web/API/FormData) as the first argument to the server action. +In this example `requestUsername` is a Server Action passed to a ``. When a user submits this form, there is a network request to the server function `requestUsername`. When calling a Server Action in a form, React will supply the form's [FormData](https://developer.mozilla.org/en-US/docs/Web/API/FormData) as the first argument to the Server Action. -By passing a server action to the form `action`, React can [progressively enhance](https://developer.mozilla.org/en-US/docs/Glossary/Progressive_Enhancement) the form. This means that forms can be submitted before the JavaScript bundle is loaded. +By passing a Server Action to the form `action`, React can [progressively enhance](https://developer.mozilla.org/en-US/docs/Glossary/Progressive_Enhancement) the form. This means that forms can be submitted before the JavaScript bundle is loaded. #### Handling return values in forms {/*handling-return-values*/} In the username request form, there might be the chance that a username is not available. `requestUsername` should tell us if it fails or not. -To update the UI based on the result of a server action while supporting progressive enhancement, use [`useFormState`](/reference/react-dom/hooks/useFormState). +To update the UI based on the result of a Server Action while supporting progressive enhancement, use [`useFormState`](/reference/react-dom/hooks/useFormState). ```js // requestUsername.js @@ -171,11 +171,11 @@ function UsernameForm() { Note that like most Hooks, `useFormState` can only be called in [client code](/reference/react/use-client). -### Calling a server action outside of `` {/*calling-a-server-action-outside-of-form*/} +### Calling a Server Action outside of `` {/*calling-a-server-action-outside-of-form*/} -Server actions are exposed server endpoints and can be called anywhere in client code. +Server Actions are exposed server endpoints and can be called anywhere in client code. -When using a server action outside of a [form](/reference/react-dom/components/form), call the server action in a [transition](/reference/react/useTransition), which allows you to display a loading indicator, show [optimistic state updates](/reference/react/useOptimistic), and handle unexpected errors. Forms will automatically wrap server actions in transitions. +When using a Server Action outside of a [form](/reference/react-dom/components/form), call the Server Action in a [transition](/reference/react/useTransition), which allows you to display a loading indicator, show [optimistic state updates](/reference/react/useOptimistic), and handle unexpected errors. Forms will automatically wrap Server Actions in transitions. ```js {9-12} import incrementLike from './actions'; @@ -212,4 +212,4 @@ export default async incrementLike() { } ``` -To read a server action return value, you'll need to `await` the promise returned. \ No newline at end of file +To read a Server Action return value, you'll need to `await` the promise returned.