【问题标题】:withFormik how to access wrapped form's props in handleSubmitwithFormik如何在handleSubmit中访问包装表单的道具
【发布时间】:2018-09-15 21:26:02
【问题描述】:

我定义:

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

然后使用 withFormic 定义 HOC:

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

在父组件中,我指定了一个回调函数:

<Task2 callback={this.something} />

现在,我希望 handleSubmit 调用回调函数。 我会这样做

this.props.callback()

但似乎在 HOC 中没有定义 this

问题:如何在 HOC 中访问 Form1.props?

【问题讨论】:

标签: reactjs formik


【解决方案1】:

您需要将 props 作为 handleSubmit 中的第二个参数之一传递,您可以按如下方式访问 props:

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

【讨论】:

    猜你喜欢
    • 2019-11-18
    • 2019-10-15
    • 2019-11-05
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多