【问题标题】:redux-form fields not rerendering/updating after form loads表单加载后 redux-form 字段不重新呈现/更新
【发布时间】:2017-01-18 19:37:46
【问题描述】:

我在让我的登录表单字段验证与 redux-form 一起使用时遇到问题。目前,我只是想让同步验证正常工作,以便检查基本错误。问题是该表单似乎只在组件安装时检查验证,然后不再检查。这是我的代码:

import React from 'react';
import { Field, reduxForm } from 'redux-form';
import * as Redux from 'react-redux';

import Input from './../../helpers/Input';
import Button from './../../helpers/Button';

export const Signup = React.createClass({

    renderInput({ label, type, input: { value }, meta: { touched, error }}){
        return (
            <Input label={ label }
                   type={ type }
                   filled={ value ? true : false }
                   touched={ touched }
                   error={ error } />
        );
    },

    render(){

        const { handleSubmit } = this.props;

        return(
            <div>
                <form onSubmit={ handleSubmit }>
                    <Field name="email" label="Email" component={ this.renderInput } />
                    <Field name="username" label="Username" component={ this.renderInput } />
                    <Field name="password" label="Password" type="password" component={ this.renderInput } />
                    <Field name="confirmPassword" label="Confirm Password" type="password" component={ this.renderInput } />
                    <Button type="submit" btnType="main" btnIcon="" btnText="Create Account" />
                </form>
            </div>
        );
    }
});

export const validate = values => {
    const errors = {};
      if (!values.username) {
          errors.username = 'Required';
      } else if (values.username.length > 15) {
          errors.username = 'Must be 15 characters or less';
      }

      if (!values.email) {
          errors.email = 'Required';
      } else if (!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(values.email)) {
          errors.email = 'Invalid email address';
      }
      console.log(errors);
      return errors;
};

export default reduxForm({
  form: 'signup',
  validate
})(Signup);

我觉得我在这里遗漏了一些非常基本的东西,但我很难过。我觉得应该分派一个动作来切换“触摸”属性 onBlur (从而重新渲染表单),但它似乎没有这样做,我在阅读 redux-form 文档时找不到类似的东西.有什么想法吗?

【问题讨论】:

    标签: forms reactjs redux redux-form


    【解决方案1】:

    您没有将 onChange 处理程序传递给您的 Input 组件,因此 redux-form 不知道值已更改。


    问题出在这里:

    renderInput({ label, type, input: { value }, meta: { touched, error }}){
        return (
            <Input label={ label }
                   type={ type }
                   filled={ value ? true : false }
                   touched={ touched }
                   error={ error } />
        );
    },
    

    要解决这个问题,请将 input 作为属性传递给您的 Input 组件,并像这样定义它,例如:

    <div className="input-row">
      <input {...input} type="text"/>
      {touched && error && 
      <span className="error">{error}</span>}
    </div>
    

    不要忘记将函数签名更改为 renderInput({ label, type, input, meta: { touched, error }}) { ... } - value 已在此处删除。


    作为替代方案,您可以显式传递onChange

     renderInput({ label, type, input: { value, onChange }, meta: { touched, error }}){
            return (
                <Input label={ label }
                       onChange = { onChange }
                       type={ type }
                       filled={ value ? true : false }
                       touched={ touched }
                       error={ error } />
            );
        },
    

    然后在您的&lt;Input ... /&gt; 中使用onChange

    【讨论】:

    • 我希望我能投票 100 次。谷歌搜索了一个小时。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-09
    • 1970-01-01
    • 2016-12-19
    • 2023-03-09
    • 2019-12-23
    相关资源
    最近更新 更多