TypeScript: pass result of useForm as component props
Hi,
I am trying to pass my
form created by useForm via props to a child component but I am not sure how to properly type it in the TypeScript.
Here is an example:
Type error shown:
Type 'ReactFormExtendedApi<{ emailAddress: string; }, FormValidateOrFn<{ emailAddress: string; }> | undefined, FormValidateOrFn<{ emailAddress: string; }> | undefined, ... 6 more ..., unknown>' is not assignable to type 'ReactFormExtendedApi<unknown, FormValidateOrFn<unknown> | undefined, FormValidateOrFn<unknown> | undefined, FormAsyncValidateOrFn<unknown> | undefined, ... 5 more ..., unknown>'.
10 Replies
solid-orangeOP•3mo ago
I understand that the problem is with Child type notation:
It should probably be something like this:
Unfortunately
ReactFormExtendedApi type expects 10 type arguments.
Generic type 'ReactFormExtendedApi' requires 10 type argument(s). (ts 2314)Has anyone tried this already? Is there a better/easier way? Or do I need to fill in all the 10 type arguments?
genetic-orange•3mo ago
withForm allows you to safely type the form prop
and you won‘t need to type the generics yourself
solid-orangeOP•3mo ago
Can you provide an example, please? Or point me to the documentation?
genetic-orange•3mo ago
Sure, one sec
genetic-orange•3mo ago
Form Composition | TanStack Form React Docs
A common criticism of TanStack Form is its verbosity out-of-the-box. While this can be useful for educational purposes helping enforce understanding our APIs it's not ideal in production use cases. As...
solid-orangeOP•3mo ago
Thanks for the link. I have read the documentation but I think that this time this is not something which can help me.
I am trying to pass
form into child component and I need kind of generic way of doing this as for instance my submit button is not rendered in parent component but I want it to be "hidden" as an implementation detail in my child component.
I ended up with custom type as I didn't really need to do anything with the data - I only needed handleSubmit and Subscribe, this is what I ended up with:
Thanks anyway!genetic-orange•3mo ago
oh, this sounds like a classic example of a form component
same docs section, where you can just use any form through context
in that case, accessing its non-values state
solid-orangeOP•3mo ago
Thank you for your reply!
I am not sure if I can imagine what you suggest. Would you be able to give me some code example? (does not to be proper code, just pseudo code).
This is not urgent as I already have a workaround. If you would find few minutes of your time that would be great.
I am using MUI 7 Dialog component (https://mui.com/material-ui/react-dialog/)
Basically my structure looks like this:
The
<form /> uses handleSubmit() from props.form and <DialogActions /> contains Subscribe for rendering submit button from props.form as well.
Correct me if I am mistaken since I am not sure how exactly this works, but wouldn't I need custom hook for each form I would like to render within my <CustomDialog /> component?React Dialog component - Material UI
Dialogs inform users about a task and can contain critical information, require decisions, or involve multiple tasks.
solid-orangeOP•3mo ago
In the meantime I am going to RTFM - I have a feeling that just quickly skimming through few sections is not sufficient.
I am sorry for wasting your time as I believe that everything is already written down in the documentation.
genetic-orange•3mo ago
with form composition, you can use
useFormContext and get a general form API to work with. This doesn't include the form's values (as any form could use them), but it includes most meta state and sections you need.
since it's context, you can also nest them
so you can have a form SubmitButton inside a form Dialog wrapper without problem
what usually confuses people is that this context is stable, so make sure to use reactive state access like form.Subscribe