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: website/docs/execute-actions/hooks/usestateaction.md
+2-65Lines changed: 2 additions & 65 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,73 +16,10 @@ You can access the documentation for the deprecated `useStateAction()` hook in t
16
16
17
17
### From v8 onwards
18
18
19
-
The `useStateAction()` hook has been deprecated in favor of the [`useActionState`](https://react.dev/reference/react/useActionState) hook from React, which was used under the hood. This is because the usage of `useStateAction`, while adding useful features, prevented progressive enhancement from working, since it wrapped the `useActionState` hook with additional functionality that only worked with JavaScript enabled.
19
+
The `useStateAction()` hook has been deprecated in favor of the [`useActionState()`](https://react.dev/reference/react/useActionState) hook from React, which was used anyway under the hood. This is because the `useStateAction()` hook, while adding useful features, prevented progressive enhancement from working, since it wrapped the `useActionState()` hook with additional functionality that only worked with JavaScript enabled.
20
20
21
21
Note that you can also use "stateless" actions with forms, as described in [this section](/docs/recipes/form-actions#stateless-form-actions).
22
22
23
23
### Example
24
24
25
-
1. Define a new stateful action called `statefulAction`, that takes a name as input and returns the name you just passed, as well as the previous one (if any).
26
-
27
-
Note two important things:
28
-
1. We're defining an action that will be used as a Form Action, so here we use the [`zod-form-data`](https://www.npmjs.com/package/zod-form-data) library to generate the input validation schema;
29
-
2. We use [`stateAction()`](/docs/define-actions/instance-methods#action--stateaction) instance method to define the action. You **must** use this method, because `useActionState()` hook requires `prevResult` to be the first argument of the Server Action function. Using this method also allows you to access the previous action result in `serverCodeFn`, via the `prevResult` property in the second argument of the function:
30
-
31
-
```typescript title=src/app/stateful-action.ts
32
-
"use server";
33
-
34
-
import { actionClient } from"@/lib/safe-action";
35
-
import { z } from"zod";
36
-
import { zfd } from"zod-form-data";
37
-
38
-
const inputSchema =zfd.formData({
39
-
name: zfd.text(z.string().min(1).max(20)),
40
-
});
41
-
42
-
exportconst statefulAction =actionClient
43
-
.metadata({ actionName: "statefulAction" })
44
-
.inputSchema(inputSchema)
45
-
// Note that we need to explicitly give a type to `stateAction` here,
46
-
// for its return object. This is because TypeScript can't infer the
47
-
// return type of the function and then "pass it" to the second argument
48
-
// of the server code function (`prevResult`). If you don't need to
49
-
// access `prevResult`, though, you can omit the type here, since it
50
-
// will be inferred just like with `action` method.
Take a look at [this section](/docs/recipes/form-actions#stateful-form-actions) of the documentation for an example of how to use the `useActionState()` hook to create a stateful action.
Copy file name to clipboardExpand all lines: website/docs/getting-started.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,7 +37,7 @@ This is a basic client, without any options or middleware functions. If you want
37
37
38
38
### 2. Define a new action
39
39
40
-
This is how a safe action is created. Providing a validation input schema to the function via [`inputSchema()`](/docs/define-actions/instance-methods#inputSchema), we're sure that data that comes in is type safe and validated.
40
+
This is how a safe action is created. Providing a validation input schema to the function via [`inputSchema()`](/docs/define-actions/instance-methods#inputschema), we're sure that data that comes in is type safe and validated.
41
41
The [`action()`](/docs/define-actions/instance-methods#action--stateaction) method lets you define what happens on the server when the action is called from client, via an async function that receives the parsed input and context as arguments. In short, this is your _server code_. **It never runs on the client.
42
42
43
43
In this documentation, we'll use the Zod library to define our validation logic, but feel free to use any other library that implements the [Standard Schema](https://github.com/standard-schema/standard-schema) specification.
Copy file name to clipboardExpand all lines: website/docs/migrations/v6-to-v7.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -137,7 +137,7 @@ By default, next-safe-action v7 returns validation errors in an object of the sa
137
137
138
138
As already said above, by default version 7 now returns validation errors in the same format of the Zod's [`format`](https://zod.dev/ERROR_HANDLING?id=formatting-errors) method.
139
139
140
-
This is customizable by using the `handleValidationErrorsShape`/`handleBindArgsValidationErrorsShape` optional functions in `schema`/`bindArgsSchemas` methods. Check out [this page](/docs/define-actions/validation-errors#customize-validation-errors-format) for more information. For example, if you need to work with flattened errors for a specific action, next-safe-action conveniently provides two functions to do that: [`flattenValidationErrors` and `flattenBindArgsValidationErrors`](/docs/define-actions/validation-errors#flattenvalidationerrors-and-flattenbindargsvalidationerrors-utility-functions).
140
+
This is customizable by using the `handleValidationErrorsShape`/`handleBindArgsValidationErrorsShape` optional functions in `schema`/`bindArgsSchemas` methods. Check out [this page](/docs/define-actions/validation-errors#customize-validation-errors-format) for more information. For example, if you need to work with flattened errors for a specific action, next-safe-action conveniently provides two functions to do that: [`flattenValidationErrors` and `flattenBindArgsValidationErrors`](/docs/define-actions/validation-errors#formatvalidationerrors-utility-function).
141
141
142
142
### [Allow calling `action` method without `schema`](https://github.com/TheEdoRan/next-safe-action/issues/107)
Copy file name to clipboardExpand all lines: website/docs/migrations/v7-to-v8.md
+8-9Lines changed: 8 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -41,7 +41,7 @@ The behavior when using functions from `next/navigation` was very unclear and co
41
41
42
42
This behavior has been changed in v8. Now, when you're using functions imported from `next/navigation` in an action:
43
43
- the hooks `status` value will be `"hasNavigated"` instead of `"hasSucceeded"`;
44
-
- a new `onNavigation` callback will be triggered, both for actions and hooks, instead of `onSuccess`. This callback receives a `navigationKind` value, that indicates the type of navigation that occurred;
44
+
- a new `onNavigation()` callback will be triggered, both for actions and hooks, instead of `onSuccess()`. This callback receives a `navigationKind` value, that indicates the type of navigation that occurred;
45
45
- the `success` property of the middleware result will now be `false`, instead of `true`, if a navigation function was called in a middleware function or in the action's server code function.
46
46
47
47
```typescript
@@ -90,7 +90,7 @@ const result = await boundAction(input);
90
90
91
91
### ⚠️✨ Removal of deprecated `executeOnMount` hook option
92
92
93
-
The deprecated `executeOnMount` hook functionality has been removed in v8. Server Actions should be used only for mutations, so it doesn't make sense to execute them on mount. Or at least, it shouldn't be a common case and, above all, a library job. If you still need to do it, just use `useEffect` to trigger the execution, however you want.
93
+
The deprecated `executeOnMount` hook functionality has been removed in v8. Server Actions should be used only for mutations, so it doesn't make sense to execute them on mount. Or at least, it shouldn't be a common case and, above all, a library job. If you still need to do it, just use `useEffect()` to trigger the execution, however you want.
94
94
95
95
### ✨ Type-checked metadata
96
96
@@ -102,7 +102,7 @@ This is a big improvement in type safety over v7. Metadata is now statically typ
102
102
103
103
### ✨ Custom thrown validation error messages
104
104
105
-
The `throwValidationErrors` option now accepts both a boolean (just like in v7) and an object with a `overrideErrorMessage` function, that allows you to customize the thrown `Error` message on the client side.
105
+
The `throwValidationErrors` option now accepts both a boolean (just like in v7) and an object with a `overrideErrorMessage()` function, that allows you to customize the thrown `Error` message on the client side.
The library, since version 7.8.0, supports both input and output validation, respectively using the `schema` and `outputSchema` methods. In v8, the `schema` method has been renamed to `inputSchema` to better reflect its purpose, and avoid potential confusion.
152
+
The library, since version 7.8.0, supports both input and output validation, respectively using the `schema()` and `outputSchema()` methods. In v8, the `schema()` method has been renamed to `inputSchema()` to better reflect its purpose, and avoid potential confusion.
153
153
154
-
The `schema` method is deprecated and will be removed in a future version, but it's still available for backward compatibility. It's now just an alias for `inputSchema`:
154
+
The `schema()` method is deprecated and will be removed in a future version, but it's still available for backward compatibility. It's now just an alias for `inputSchema()`:
To update your actions, you can just use the search and replace feature of your editor to replace all occurrences of `.schema` with `.inputSchema`.
165
+
To update your actions, you can just use the search and replace feature of your editor to replace all occurrences of `.schema()` with `.inputSchema()`.
166
166
:::
167
167
168
168
169
169
### 🔄 Deprecation of `useStateAction` hook
170
170
171
-
The `useStateAction` hook has been deprecated. It's always been kind of a hack to begin with, and it doesn't support progressive enhancement, since it tries to do what the `useAction` and `useOptimisticAction` hooks do.
171
+
The `useStateAction()` hook has been deprecated. It's always been kind of a hack to begin with, and it doesn't support progressive enhancement, since it tries to do what the `useAction()` and `useOptimisticAction()` hooks do.
172
172
173
-
So, from now one, the recommended way to use stateful actions is to do it with the React's built in `useActionState` hook, as explained in [this section](#) of the documentation.
174
-
#### TODO: add link
173
+
So, from now one, the recommended way to use stateful actions is to do it with the React's built in `useActionState()` hook, as explained in [this section](/docs/recipes/form-actions#stateful-form-actions) of the documentation.
Copy file name to clipboardExpand all lines: website/docs/recipes/form-actions.md
+13-4Lines changed: 13 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -84,6 +84,8 @@ Note that if you want or need to use _stateful_ actions:
84
84
85
85
Here's an example of a stateful action, using the `useActionState()` hook:
86
86
87
+
1. Define a new stateful action called `statefulFormAction()`, that takes a name as input and returns the name you just passed, as well as the previous one (if any).
0 commit comments