【问题标题】:formik fields are not getting valueformik 字段没有获得价值
【发布时间】:2019-06-30 08:02:22
【问题描述】:

我在我的 React 和 redux 项目中使用 Formik,但是 Formik 字段没有获得价值并且 onChange 字段功能不起作用! 此表格用于编辑客户。我在项目的另一部分使用 Formik,我没有问题。我不知道该怎么办?

<Formik
            initialValues={props.selectedCustomer.id ? props.selectedCustomer : emptyCustomer}
            validationSchema={customerValidationSchema}
            onSubmit={async (values: ICustomer, actions: FormikActions<any>) => {
                console.log(values)
                try {
                    await postCustomer(values)
                    props.selectedCustomer.id ? enqueueSnackbar('success') : enqueueSnackbar('fail')
                    actions.resetForm()
                    getCustomers(pageNum, pageSize)
                    setAddDialog(false)
                }
                catch (error) {
                    enqueueSnackbar(error, { variant: 'default' })
                }
            }}
          >
          {() => (
          <Form id="addCustomerForm">
          <div id="addCustomerDiv" className={clsx({
            [classes.noDisplay]: !addDialog
          })}>
              <Grid style={{marginRight: '0.5rem'}} container spacing={5}>
                 <Grid item xs={2}>
                  <Field
                      name="firstName"
                      render={({ field, form }: FieldProps<ICustomer>) => (
                          <TextField
                              margin="dense"
                              id="firstName"
                              label="firstName"
                              fullWidth
                              {...field}
                              error={form.errors.firstName && form.touched.firstName ? true : false}
                          />
                      )}
                  />
                </Grid>         
              <Button
              type='submit'
              form="addCustomerForm"
              color="primary"
              variant="contained"
              style={{marginTop: '3rem'}}
              className={clsx({
                [classes.noDisplay]: !addDialog
              })}
              >
              submit
              </Button>
              </Grid>              
          </Grid>
          </div>
          </Form>
          )}
          </Formik>

有什么问题?

【问题讨论】:

  • 抱歉,FieldProps 的用途是什么?
  • 你能告诉我们initialValues 是什么吗?

标签: reactjs formik


【解决方案1】:

我在 Formik 组件中添加了 enableReinitialize 并且它起作用了。

【讨论】:

  • 太棒了!!!这个提示对我帮助很大!!!
猜你喜欢
  • 2016-10-20
  • 2021-03-28
  • 2016-01-30
  • 2018-04-08
  • 2018-04-09
  • 2012-06-24
  • 1970-01-01
  • 2021-06-21
  • 2021-10-31
相关资源
最近更新 更多