validation - 使用 redux-form 我在输入第一个字符后失去焦点

标签 validation reactjs redux redux-form

我正在使用 redux-form 和模糊验证。当我在输入元素中输入第一个字符后,它会失去焦点,我必须再次单击它才能继续输入。它仅对第一个字符执行此操作。后续字符类型仍然是焦点。这是我的基本登录表单示例:

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Field, reduxForm } from 'redux-form';
import * as actions from '../actions/authActions';

require('../../styles/signin.scss');


class SignIn extends Component {

  handleFormSubmit({ email, password }) {
    this.props.signinUser({ email, password }, this.props.location);
  }

  renderAlert() {
    if (this.props.errorMessage) {
      return (
        <div className="alert alert-danger">
          {this.props.errorMessage}
        </div>
      );
    } else if (this.props.location.query.error) {
      return (
        <div className="alert alert-danger">
          Authorization required!
        </div>
      );
    }
  }

  render() {

    const { message, handleSubmit, prestine, reset, submitting } = this.props;

    const renderField = ({ input, label, type, meta: { touched, invalid, error } }) => (
      <div class={`form-group ${touched && invalid ? 'has-error' : ''}`}>
        <label for={label} className="sr-only">{label}</label>
        <input {...input} placeholder={label} type={type} className="form-control" />
        <div class="text-danger">
          {touched ? error: ''}
        </div>
      </div>
    );


    return (
      <div className="row">
        <div className="col-md-4 col-md-offset-4">
          <form onSubmit={handleSubmit(this.handleFormSubmit.bind(this))} className="form-signin">
            <h2 className="form-signin-heading">
              Please sign in
            </h2>
            {this.renderAlert()}
            <Field name="email" type="text" component={renderField} label="Email Address" />
            <Field name="password" type="password" component={renderField} label="Password" />
            <button action="submit" className="btn btn-lg btn-primary btn-block">Sign In</button>
          </form>
        </div>
        </div>
    );
  }
}

function validate(values) {
  const errors = {};

  if (!values.email) {
    errors.email = 'Enter a username';
  }

  if (!values.password) {
    errors.password = 'Enter a password'
  }

  return errors;
}

function mapStateToProps(state) {
  return { errorMessage: state.auth.error }
}

SignIn = reduxForm({
  form: 'signin',
  validate: validate
})(SignIn);

export default connect(mapStateToProps, actions)(SignIn);

最佳答案

发生这种情况是因为您每次渲染时都会将 renderField 重新定义为新组件,这意味着它看起来像是 React 的一个新组件,因此它将被卸载原来的并重新安装新的。

您需要将其举起:

const renderField = ({ input, label, type, meta: { touched, invalid, error } }) => (
      <div class={`form-group ${touched && invalid ? 'has-error' : ''}`}>
        <label for={label} className="sr-only">{label}</label>
        <input {...input} placeholder={label} type={type} className="form-control" />
        <div class="text-danger">
          {touched ? error: ''}
        </div>
      </div>
    );

class SignIn extends Component {

  ...

  render() {
    const { message, handleSubmit, prestine, reset, submitting } = this.props;


    return (
      <div className="row">
        <div className="col-md-4 col-md-offset-4">
          <form onSubmit={handleSubmit(this.handleFormSubmit.bind(this))} className="form-signin">
            <h2 className="form-signin-heading">
              Please sign in
            </h2>
            {this.renderAlert()}
            <Field name="email" type="text" component={renderField} label="Email Address" />
            <Field name="password" type="password" component={renderField} label="Password" />
            <button action="submit" className="btn btn-lg btn-primary btn-block">Sign In</button>
          </form>
        </div>
        </div>
    );
  }
}

...

关于validation - 使用 redux-form 我在输入第一个字符后失去焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39839051/

相关文章:

Redux-saga路线更改的预载数据

mysql - 使用 Play2.2.0 和 Ebean 验证数据失败

c# - WinForm 用户界面验证

javascript - NavigationBar 的 routeMapper 不是函数

reactjs - relay 或 Apollo-react 如何解决涉及多重关系的突变

javascript - React Router 静默推送

reactjs - 使用 mapStateToProps 连接时“无法读取未定义的属性 'map'”

javascript - 号码验证不起作用

javascript - 如何 : Dynamically generate CSRF-Token in WTForms with Flask

javascript - 使用 Promise 和异步调用 ipinfo.io (REACT)