How can I use useFormikContext hook in my class component?

Viewed 376

I'm working with Formik forms in React Native App. I want my form to be submitted using a button that is outside the Formik component. I know how the useFormikContext hook works but the problem is with my component which is a class component (not a functional component). How can I submit my form?

1 Answers

I've to use React.createRef() in order to solve my problem. I've created the ref in the constructor like this

this.myRef = React.createRef();

then I've to apply innerRef prop to my form like this

innerRef={this.myRef}

and finally I've to call submitForm method in my handleSubmit like this

this.myRef.current?.submitForm();

That's it :)

Related