简体   繁体   English

React Formik Field onChange 事件句柄

[英]React Formik Field onChange event handle

I am trying to handle onChange for Field component in React Formik, but it doesn't work.我正在尝试在 React Formik 中处理 Field 组件的 onChange,但它不起作用。 I also tried to handle it outside Formik component by:我还尝试通过以下方式在 Formik 组件之外处理它:

handleChange(e) {
  console.log('changing');
}
<Field type="radio" name="players" value="1" onChange={e => this.handleChange(e)}/>

but I am getting the warning:但我收到警告:

A component is changing an uncontrolled input of type text to be controlled.组件正在更改要控制的文本类型的不受控制的输入。 Input elements should not switch from uncontrolled to controlled (or vice versa).输入元素不应从不受控制切换到受控制(反之亦然)。

For now my code looks like this:现在我的代码是这样的:

<Formik
  onChange={() => {
    console.log('changing');
  }}
  onSubmit={(values) => {
    console.log('submitted');
  }}
>
{({ isSubmitting, handleChange }) => (
  <Form>
    <InputWrapper>
       <span>1</span>
       <Field type="radio" name="players" value="1" onChange={handleChange}/>
       <span>2</span>
       <Field type="radio" name="players" value="2" onChange={handleChange}/>
    </InputWrapper>
    <button type="submit" disabled={isSubmitting}>
       {isSubmitting ? 'Loading..' : 'Start'}
    </button>
  </Form>
)}
</Formik>

Any tips/ideas?任何提示/想法?

One issue I have found with introducing the onBlur:handleBlur to your Formik Field is that it overrides the Formik Validation.我在将 onBlur:handleBlur 引入 Formik 字段时发现的一个问题是它覆盖了 Formik 验证。

Use onKeyUp={handleChange}使用onKeyUp={handleChange}

This solved said problem这解决了上述问题

You must to use the InputProps of Field like the following...您必须使用 Field 的 InputProps,如下所示...

import { TextField } from 'formik-material-ui';

<Field
  type="radio" 
  name="players" 
  value="2"
  component={TextField} 
  InputProps={{ onBlur:handleBlur }}/>

To use the InputProps in the Field you need to use a component TextField from the formik-material-ui lib.要在 Field 中使用 InputProps,您需要使用 formik-material-ui 库中的组件 TextField。

Another way is use the onKeyUp or onKeyDown, that functions work ok with Field and that functions are like onChange另一种方法是使用 onKeyUp 或 onKeyDown,这些函数可以与 Field 一起工作,并且函数就像 onChange

<Field 
  type="radio" 
  name="players" 
  value="2" 
  onKeyUp =this.handleChange/>

I found one trick which worked well for me, you can use "values" of formik and call a method passing the "values" as parameter and perform the action using new values.我发现了一个对我很有效的技巧,您可以使用 formik 的“值”并调用传递“值”作为参数的方法并使用新值执行操作。

const handleUserChange = (userId: number) => {
    //userId is the new selected userId
  };


 <Formik
        initialValues={initialValues}
        validationSchema={validationSchema}
        onSubmit={handleSubmit}
        enableReinitialize
      >
        {({ isValid, isSubmitting, values }) => (
          <Form>
            <table className="table">
              <tbody>
                <tr>
                  <td>Users</td>
                  <td>
                    <Field name="userId" className="form-control" as="select">
                      <option value="">--select--</option>
                      {data.Users.map((user, index) => (
                        <option key={user.id} value={user.id}>{`User ${index + 1}`}</option>
                      ))}
                    </Field>
                  </td>
                </tr>
                {handleUserChange(values.userId)}
              </tbody>
            </table>

            <div className="row">
              <div className="col-sm-12">
                <SubmitButton label="Save" submitting={isSubmitting} disabled={!isValid || isSubmitting} />
              </div>
            </div>
          </Form>
        )}
      </Formik>

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

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