Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset a React Native Formik form from outside the form

I know you can use resetForm in Formik to reset the form in onSubmit. However, is there a way to reset it programmatically by assigning it a ref and calling a rest form method on it somehow?

like image 928
Ben Avatar asked Feb 19 '26 12:02

Ben


1 Answers

there is a prop called innerRef to make ref for a formik form

<Formik
   innerRef={(p) => (this.formik = p)}
/>

then use it like this:

onPress={() => this.formik.resetForm()}

Or with a functional component:

const formikRef = useRef();
<Formik
  innerRef={formikRef}
  ...
/>

and then use with

onPress={() => formikRef.current?.resetForm()}
like image 141
Hassan Kandil Avatar answered Feb 21 '26 03:02

Hassan Kandil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!