Could useField
take a defaultValue
?
#436
aaronadamsCA
started this conversation in
Ideas
Replies: 1 comment 1 reply
-
The way I've seen this done in this and other form-helper libraries is with a function you'd pass to defaultValue. const [form, fields] = useForm({
defaultValue: (user?: UserSchema) => ({
id: user?.id ?? '',
address: {
street: user?.address.street ?? '',
isPublic: user?.address.street ?? true
}
})
}); I haven't tested it, but I think that'll mean that your useField hooks will inherit whatever value you set up in the useForm hook. Does that solve your issue? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We have a lot of fieldsets that get reused between "new" forms and "edit" forms:
What I'd love is some way to set these initial values at the fieldset level, to be used only if the form has no
defaultValue
.My idea was a
defaultValue
option on theuseField
hook:useForm
was called withdefaultValue === undefined
, thenuseField
would use itsdefaultValue
.useForm
was called withdefaultValue !== undefined
, thenuseField
would ignore itsdefaultValue
.I wouldn't want or expect anything more complex than this—definitely no merging of values. This would be super simple; the
defaultValue
passed touseField
would apply if and only if nodefaultValue
was passed touseForm
.I don't know if this suggested API is the best one, but I think this very common use case would be great (and hopefully straightforward) to support. What we're doing right now is adding a
defaultValue
property to each fieldset, then manually adding it to thedefaultValue
passed touseForm
; the problem with this, of course, is that it's easy to forget.Thanks!
Beta Was this translation helpful? Give feedback.
All reactions