【问题标题】:How to call OnChange function in react using withformik with antd component?如何在使用 withformik 和 antd 组件时调用 OnChange 函数?
【发布时间】:2019-03-15 08:24:17
【问题描述】:

我在这里调用 Formik Field 上的 onChange 函数,但它没有调用?如何在 Formik 字段上调用自定义函数?

这是我在 React Component 下的自定义函数:

onStudentScore = (value, form) => {
  alert("called");
  const maxScore = value.writtenexammaxscore;
  console.log(maxScore);
  form.getFieldValue("writtenexammaxscore", maxScore);
  if (maxScore > form.getFieldValue("writtenexamstudentsscore")) {
    alert("MaxScore is less than StudentScore");
  }
};

我的表单是在渲染下创建的,并在 StudentScore 字段上编写 onChange 函数。但它不叫?如何调用这个函数?

  render() {
        const { values, handleSubmit} = this.props
        return (
      return (
        <div>
          <h5 align="left">MidTerm Form</h5>
          <Card>
            <Form onSubmit={handleSubmit}>
              <Row>
                <Col span={4}>
                  <b>Written Exam:</b>
                </Col>
                <Col span={2}>
                  <Field
                    name="writtenexammaxscore"
                    component={AntInput}
                    type="text"
                    style={{ width: 40 }}
                  />
                </Col>
                <Col span={2}>outof</Col>
                <Col span={3}>
                  <Field
                    name="writtenexamstudentsscore"
                    component={AntInput}
                    type="text"
                    style={{ width: 40 }}
                    onChange={this.onStudentScore}
                  />
                  // I wrote the function on field this way
                </Col>

                <Col span={2}>
                  <Divider type="vertical" />
                </Col>
              </Row>

              <Row>
                <Col span={10} />
                <Col span={8} push={10}>
                  <Button type="primary" htmlType="submit">
                    Submit
                  </Button>
                </Col>
              </Row>
            </Form>
          </Card>
        </div>
      );
    }
const MidTermForm = withFormik({

    mapPropsToValues: () => ({
        writtenexammaxscore: '',
        writtenexamstudentsscore: '',
        oralexammaximumscore: '',
        oralexamstudentsscore: '',



    }),
    handleSubmit(values, { resetForm }) {
        resetForm();
        console.log(values)
    }


})(MidTermFormComponent)

export default MidTermForm

【问题讨论】:

  • 我没有看到 Formik 在您的代码中使用,只有字段,没有 Formik 将无法使用。
  • 我使用 withformik 封装了函数并从 Antd 获取组件
  • 现在,当您将其添加到代码中时,我看到了 :)
  • 好的,为什么不调用该函数?

标签: reactjs antd formik yup


【解决方案1】:

我尝试扩展 yup 验证模式。而不是在 onChange 中调用函数 检查这个code sandbox

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 2021-05-16
    • 1970-01-01
    相关资源
    最近更新 更多