-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
66 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type { HookActionStatus } from "./hooks"; | ||
|
||
/** | ||
* Returns true if the action is idle. | ||
* @param status | ||
* @returns | ||
*/ | ||
export function isIdle(status: HookActionStatus): status is "idle" { | ||
return status === "idle"; | ||
} | ||
|
||
/** | ||
* Returns true if the action is executing. | ||
* @param status | ||
* @returns | ||
*/ | ||
export function isExecuting(status: HookActionStatus): status is "executing" { | ||
return status === "executing"; | ||
} | ||
|
||
/** | ||
* Returns true if the action has succeeded. | ||
* @param status | ||
* @returns | ||
*/ | ||
export function hasSucceeded(status: HookActionStatus): status is "hasSucceeded" { | ||
return status === "hasSucceeded"; | ||
} | ||
|
||
/** | ||
* Returns true if the action has errored. | ||
* @param status | ||
* @returns | ||
*/ | ||
export function hasErrored(status: HookActionStatus): status is "hasErrored" { | ||
return status === "hasErrored"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
website/docs/usage/client-components/hooks/checking-action-status.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
sidebar_position: 4 | ||
description: Learn how to check the current action status. | ||
--- | ||
|
||
# Checking action status | ||
|
||
There are two ways to get the current action status: | ||
|
||
1. Directly via the `status` property returned by `useAction` and `useOptimisticAction` hooks. | ||
2. Using utility functions imported from `next-safe-action/status`. | ||
|
||
## Utility functions | ||
|
||
You can import `isIdle`, `isExecuting`, `hasSucceeded`, and `hasErrored` from `next-safe-action/status`. This emulates the pre v4 implementation, and it's similar to the [Tanstack Query](https://tanstack.com/query/latest) API. | ||
|
||
### Why using single `status` property is better? | ||
|
||
The reason for the v4 change, other than debloating the hook result object, is that using a single `status` string property is discriminating, i.e. **only one** status can be active at a specific time. Instead, returning multiple functions to check status as before v4, doesn't guarantee the same behavior. | ||
|
||
If you use the utility functions imported from `next-safe-action/status` you get the same discriminated status string, thanks to TypeScript's [type predicates](https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates) feature. |