What is the best practice to submit a form the parent component? #2966
-
I often put a form in a dialog with an OK button. If I can put a useForm on top of the dialog, it's fine, but it's not practical to call useForm in the dialog itself since I want to display the form after the dialog is displayed and the data fetched. I want to submit the form from the callback of the parent dialog, but how would it be more elegant to submit it? const MyForm: React.FC<{defaultValues: any}> = ({defaultValues}) => {
const {register} = useForm({defaultValues});
return <form>
<input name="name1" ref={register} />
</form>;
}
export const MyDialog: React.FC = () => {
const {isLoading, data} = useMyFormInitialDataQuery();
return <Dialog onOk={howToSubmitFormElegantly()}> {/* here! */}
{isLoading && <Spinner />}
{!isLoading && <MyForm defaultValues={data} />}
</Dialog>;
} |
Beta Was this translation helpful? Give feedback.
Answered by
bluebill1049
Sep 22, 2020
Replies: 1 comment 3 replies
-
|
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
sunnyone
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
useForm
probably should be at the parent level, and then you can pass down props to your child.