简体   繁体   English

使用Formik验证的onChange

[英]onChange using Formik Validation

I'm currently having getting my input values to appear on screen as im typing. 我目前正在输入输入值,以便在即时输入时显示在屏幕上。 For example, I have a form that requires a first name and last name. 例如,我有一个要求输入名字和姓氏的表格。 Upon typing in those values I am trying to display the inputs typed onto the DOM. 输入这些值后,我试图显示在DOM上键入的输入。 This was successful earlier with an onChange, and using this.state.firstName, this.state.lastName. 之前使用onChange并使用this.state.firstName,this.state.lastName可以成功完成此操作。 I have implemented formik validation and currently my inputs only appear after I upload an image which has a set state. 我已经实现了Formik验证,目前只有在上传具有设置状态的图像后,我的输入才会显示。

I have an onChange passing its values 我有一个onChange传递其值

     handleChange = event => {
     this.setState({ [event.target.name]: event.target.value });
     };      
      <---->
       <Formik
        initialValues={this.state.formData}
        enableReinitialize={true}
        validationSchema={userProfileValidation}
        onSubmit={this.handleSubmit}
        render={formikProps => (
          <div className="container-fluid">
            <div className="edit-profile">
              <div className="row">
                <div className="col-lg-4">
                  <div className="card">
                    <div className="card-header">
                      <h3 className="card-title">My Profile</h3>
                    </div>
                    <div className="card-body">
                      <div className="row mb-2">
                        <div className="col-auto">
                          <img
                            className="img-90 rounded-circle"
                            alt=""
                            src={
                              this.state.formData.avatarUrl
                                ? this.state.formData.avatarUrl
                                : "https://iupac.org/wp- 
                 content/uploads/2018/05/default-avatar.png"
                            }
                          />
                        </div>
                        <div className="col">
                          <h4 className="mb-1">
                            {this.state.formData.firstName} {""}{" "}
                            {this.state.formData.mi} {""}{" "}
                            {this.state.formData.lastName}{" "}
                          </h4>
                        </div>
                      </div>
                    </div>
                  </div>

I am able to show what input on a setState but live like it previously shown. 我能够显示setState上的哪些输入,但是像以前显示的那样运行。

You're trying to mix two separate things. 您正在尝试混合两种不同的东西。 Formik exist so that you don't have to manage your component level form state by yourself doing so is hard and formik does that for you. Formik存在,因此您不必自己管理组件级别的表单状态,这很困难,而Formik会为您做到这一点。

You should pass an object containing form initial field values to initialValues prop instead of this.state.formData 您应该将包含表单初始字段值的对象传递给initialValues prop而不是this.state.formData

To update a DOM value based on a field input somewhere, you can do this 要基于某处的字段输入来更新DOM值,您可以执行此操作

<Field
    name="email"
    type="email"
    onChange={e => {
        // call the built-in handleChange for formik
        handleChange(e)
        // and do something about e
        let someValue = e.currentTarget.value
        this.updateEmailField(someValue) // Update a DOM element on this function
        ...
    }}
/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM