-
Notifications
You must be signed in to change notification settings - Fork 619
fix: provide default empty array for watched form fields #5196
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Oops, something went wrong.
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.
🧩 Analysis chain
🌐 Web query:
React Hook Form v7 watch method defaultValue parameter behavior when field value is null or invalid type💡 Result:
In React Hook Form v7,
watch(name, defaultValue)(anduseWatch({ name, defaultValue })) usesdefaultValueonly as a fallback when the watched field is not yet set / returnsundefined(commonly on the first render before the field is registered, or when a value doesn’t exist in form state). It’s not a general “nullish coalescing” fallback. [1] [2] [3]So, for your cases:
null:watch()returnsnull(because the field is set), and thedefaultValueparameter is not applied. RHF generally expects you to usenullor""as the “cleared” value rather thanundefined. [4]watch()returns whatever is currently stored in RHF’s form state—it does not coerce types based ondefaultValue, and validation errors don’t change whatwatch()returns. (Type-safety is primarily a TypeScript concern at compile time, not runtime.) [1] [3]undefined: RHF treats that like “not set” and you’ll observe fallback behavior to defaults (either thewatch/useWatchdefaultValue, oruseForm({ defaultValues })). Also, setting a field toundefinedeffectively can snap it back to its default. [1] [5]Practical rule: if you need a fallback when a value is
null, do it yourself:const v = watch('x'); const resolved = v ?? myFallback;(or handlenullexplicitly), because RHF’sdefaultValueis primarily for the undefined/unregistered case. [1] [3]Sources
[1] https://reacthookform.caitouyun.com/api/useform/watch
[2] https://react-form-hook.com/docs/usewatch
[3] https://app.unpkg.com/react-hook-form@7.70.0/files/dist/watch.d.ts
[4] https://react-form-hook.com/docs/usecontroller/controller
[5] https://github.com/orgs/react-hook-form/discussions/5858
🏁 Script executed:
Repository: unkeyed/unkey
Length of output: 2907
🏁 Script executed:
wc -l "web/apps/dashboard/app/(app)/[workspaceSlug]/apis/[apiId]/keys/[keyAuthId]/_components/components/table/components/actions/components/edit-rbac/index.tsx"Repository: unkeyed/unkey
Length of output: 214
🏁 Script executed:
Repository: unkeyed/unkey
Length of output: 84
🏁 Script executed:
Repository: unkeyed/unkey
Length of output: 84
🏁 Script executed:
fd "edit-rbac" --type fRepository: unkeyed/unkey
Length of output: 213
🏁 Script executed:
cat -n "web/apps/dashboard/app/(app)/[workspaceSlug]/apis/[apiId]/keys/[keyAuthId]/_components/components/table/components/actions/components/hooks/use-edit-rbac.ts"Repository: unkeyed/unkey
Length of output: 3627
🏁 Script executed:
Repository: unkeyed/unkey
Length of output: 5871
🏁 Script executed:
fd "update-key-rbac.schema" --type fRepository: unkeyed/unkey
Length of output: 226
🏁 Script executed:
fd "use-persisted-form" --type fRepository: unkeyed/unkey
Length of output: 153
🏁 Script executed:
cat -n "web/apps/dashboard/app/(app)/[workspaceSlug]/apis/[apiId]/keys/[keyAuthId]/_components/components/table/components/actions/components/edit-rbac/update-key-rbac.schema.ts"Repository: unkeyed/unkey
Length of output: 597
🏁 Script executed:
cat -n "web/apps/dashboard/hooks/use-persisted-form.tsx"Repository: unkeyed/unkey
Length of output: 3627
Normalize array fields at read-time to handle corrupted persisted state
The
watch(..., [])defaultValue only applies when a field isundefined. If persisted state contains malformed values (null, non-array types),watch()will return the corrupted value, and downstream operations (Line 113:.includes(), Line 116: spread) will fail or misbehave. SinceusePersistedFormdirectly restores JSON without schema validation, add runtime normalization when accessing these watched values.Normalize array fields
This aligns with the coding guideline to parse inputs at boundaries into typed structures, preventing illegal states from propagating through the component logic.
📝 Committable suggestion
🤖 Prompt for AI Agents