I have been asked this question several times recently:
-
I am using Formik in my React project
-
I want to observe (or listen to) the changes of form values
-
But
does not provide a prop for onChange callback
How do I do that?
Well, I would like to share two options:
-
#1 Attach callback function on
<form>
element / component -
#2 Create a listener / observer component with useFormikContext
Solution 1: Attach callback on <form>
Formik does not provide onChange prop, but <form>
(or <Form>
if using the wrapper version) does provide it.
So, you can just define a handleOnChange callback function like this:
const handleOnChange = (event: FormEvent) => {
console.log("Form::onChange", event);
};
… and pass it directly to the