-
-
Notifications
You must be signed in to change notification settings - Fork 0
docs(mutations): document return type semantics for mutation hooks #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -310,8 +310,15 @@ export interface MutationState<TData> { | |||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| export interface MutationOptions { | ||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * If true, errors will be rethrown instead of being caught and stored in state. | ||||||||||||||||||||||||||||||
| * This allows callers to use try/catch for error handling. | ||||||||||||||||||||||||||||||
| * Controls error handling behavior for the mutation. | ||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * - **`false` (default)**: Errors are caught and stored in the `error` state. | ||||||||||||||||||||||||||||||
| * The `mutate` function returns `undefined` on error. | ||||||||||||||||||||||||||||||
| * Use the `isError` flag and `error` state to handle failures reactively. | ||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * - **`true`**: Errors are rethrown, allowing try/catch error handling. | ||||||||||||||||||||||||||||||
| * The `mutate` function throws on error. | ||||||||||||||||||||||||||||||
| * Use this when you need imperative error handling. | ||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * @default false | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
|
|
@@ -322,19 +329,70 @@ export interface MutationOptions { | |||||||||||||||||||||||||||||
| * Return value of mutation hooks. | ||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * @remarks | ||||||||||||||||||||||||||||||
| * Combines mutation state with a `mutate` trigger function accepting a payload, along with convenient booleans for status. | ||||||||||||||||||||||||||||||
| * Combines mutation state with a `mutate` trigger function accepting a payload, | ||||||||||||||||||||||||||||||
| * along with convenient booleans for status checking. | ||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * ## Return Value Semantics | ||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * The `mutate` function returns `Promise<TData | undefined>`: | ||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * - **On success**: Returns the mutation result data (`TData`) | ||||||||||||||||||||||||||||||
| * - **On error with `throwOnError: false` (default)**: Returns `undefined` and stores error in `error` state | ||||||||||||||||||||||||||||||
| * - **On error with `throwOnError: true`**: Throws the error (use try/catch) | ||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * ## Recommended Patterns | ||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * ```ts | ||||||||||||||||||||||||||||||
| * // Pattern 1: Check return value (when throwOnError is false) | ||||||||||||||||||||||||||||||
| * const result = await mutate(payload); | ||||||||||||||||||||||||||||||
| * if (result === undefined) { | ||||||||||||||||||||||||||||||
| * // Check error state | ||||||||||||||||||||||||||||||
| * console.error('Mutation failed:', error); | ||||||||||||||||||||||||||||||
| * } else { | ||||||||||||||||||||||||||||||
| * // Use result | ||||||||||||||||||||||||||||||
| * console.log('Created:', result); | ||||||||||||||||||||||||||||||
| * } | ||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * // Pattern 2: Use try/catch (when throwOnError is true) | ||||||||||||||||||||||||||||||
| * try { | ||||||||||||||||||||||||||||||
| * const result = await mutate(payload); | ||||||||||||||||||||||||||||||
| * console.log('Created:', result); | ||||||||||||||||||||||||||||||
| * } catch (err) { | ||||||||||||||||||||||||||||||
| * console.error('Mutation failed:', err); | ||||||||||||||||||||||||||||||
| * } | ||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * // Pattern 3: Use status flags (reactive) | ||||||||||||||||||||||||||||||
| * if (isSuccess) { | ||||||||||||||||||||||||||||||
| * console.log('Created:', data); | ||||||||||||||||||||||||||||||
| * } | ||||||||||||||||||||||||||||||
| * if (isError) { | ||||||||||||||||||||||||||||||
| * console.error('Failed:', error); | ||||||||||||||||||||||||||||||
|
Comment on lines
+364
to
+369
|
||||||||||||||||||||||||||||||
| * // Pattern 3: Use status flags (reactive) | |
| * if (isSuccess) { | |
| * console.log('Created:', data); | |
| * } | |
| * if (isError) { | |
| * console.error('Failed:', error); | |
| * // Pattern 3: Use status flags (reactive) from the mutation result | |
| * const mutation = useCreateVariable(); | |
| * | |
| * if (mutation.isSuccess) { | |
| * console.log('Created:', mutation.data); | |
| * } | |
| * if (mutation.isError) { | |
| * console.error('Failed:', mutation.error); |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Pattern 1, the variable
erroris referenced without context. The code example showsconsole.error('Mutation failed:', error);buterroris not in scope within this standalone example - it's a property of the MutationResult object. This should be shown as accessing the error from the mutation result object (e.g., destructured from the hook or accessed via dot notation) to provide accurate guidance to developers.