-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(react-start): Update useServerFn return type to be undefined after redirect #6295
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,7 +1,7 @@ | ||||||||||||||||||||||||||||||
| import * as React from 'react' | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| export function useMutation<TVariables, TData, TError = Error>(opts: { | ||||||||||||||||||||||||||||||
| fn: (variables: TVariables) => Promise<TData> | ||||||||||||||||||||||||||||||
| fn: (variables: TVariables) => Promise<TData | void> | ||||||||||||||||||||||||||||||
| onSuccess?: (ctx: { data: TData }) => void | Promise<void> | ||||||||||||||||||||||||||||||
| }) { | ||||||||||||||||||||||||||||||
| const [submittedAt, setSubmittedAt] = React.useState<number | undefined>() | ||||||||||||||||||||||||||||||
|
|
@@ -20,11 +20,13 @@ export function useMutation<TVariables, TData, TError = Error>(opts: { | |||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||
| const data = await opts.fn(variables) | ||||||||||||||||||||||||||||||
| await opts.onSuccess?.({ data }) | ||||||||||||||||||||||||||||||
| setStatus('success') | ||||||||||||||||||||||||||||||
| setError(undefined) | ||||||||||||||||||||||||||||||
| setData(data) | ||||||||||||||||||||||||||||||
| return data | ||||||||||||||||||||||||||||||
| if (data) { | ||||||||||||||||||||||||||||||
| await opts.onSuccess?.({ data }) | ||||||||||||||||||||||||||||||
| setStatus('success') | ||||||||||||||||||||||||||||||
| setError(undefined) | ||||||||||||||||||||||||||||||
| setData(data) | ||||||||||||||||||||||||||||||
| return data | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+23
to
+29
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Truthiness check will fail for valid falsy return values. The 🔎 Proposed fix using explicit undefined check- if (data) {
+ if (data !== undefined) {
await opts.onSuccess?.({ data })
setStatus('success')
setError(undefined)
setData(data)
return data
}Additional concern: When 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||
| } catch (err) { | ||||||||||||||||||||||||||||||
| setStatus('error') | ||||||||||||||||||||||||||||||
| setError(err as TError) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
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.
Truthiness check will fail for valid falsy return values.
The
if (data)check on line 23 uses JavaScript truthiness, which evaluates tofalsefor valid data values like0,false,"", orNaN. This will incorrectly skip success handling whenTDatais a falsy type.🔎 Proposed fix using explicit undefined check
Additional concern: When
dataisundefined(redirect scenario), the status remains'pending'indefinitely. Consider whether the status should transition to a different state (e.g.,'idle'or a new'redirected'state) to accurately reflect the mutation's completion.Note: This file is nearly identical to
examples/react/start-supabase-basic/src/hooks/useMutation.ts. Consider consolidating these duplicate implementations into a shared utility.🤖 Prompt for AI Agents