withFormik how to access wrapped form's props in handleSubmit

Viewed 4458

I define:

class Form1 extends React.Component{
....
}

and then define the HOC using withFormic:

const Form2 = withFormik({
  handleSubmit(values, { resetForm, setErrors, setSubmitting }) {
    ...
  },
....
})(Form1);

In the parent component, I specify a callback function:

<Task2 callback={this.something} />

Now, I want the handleSubmit to call the callback function. I would do just

this.props.callback()

but it seems that this is not defined in the HOC.

Question: how can I access the Form1.props within the HOC?

1 Answers

You need to pass props as one of the second argument in handleSubmit and you can access props as below:

const Form2 = withFormik({
handleSubmit(values, { props, resetForm, setErrors, setSubmitting }) {
...
props.callback();
},
....
})(Form1);
Related