TanStackT
TanStack3mo ago
6 replies
popular-magenta

deriving field state value from bigger (field state?) objects

hello

in my form i'd like to have an intermediate object between what i submit and what i display.

Take the use-case of a "user search" input. you'd like to keep the whole user object in the field state to display the user name in the input, although you might want to submit (and validate) the user id only.

i could either:
1. have the user object in the defaultValues rather than the userId, but then my onSubmit cannot pass value seamlessly (need to { ...value, userId: value.user.id })
2. have a separate state for this, which works but is ugly

i was wondering if there's a way to store this additional data (in the store maybe) and wire back the "userId" as a getter of sort.

thanks !
Was this page helpful?