Using redux-form I'm losing focus after typing the first character

后端 未结 7 629
甜味超标
甜味超标 2020-12-24 12:40

I\'m using redux-form and on blur validation. After I type the first character into an input element, it loses focus and I have to click in it again to continue

相关标签:
7条回答
  • 2020-12-24 13:03

    I had the same problem, and none of the answers worked for me. But thanks to Advem's answer I got an idea of what could be wrong: My form required accordion UI, and for that I had state variable in it:

    const ConveyorNotificationSettingsForm = (props) => {
        const {handleSubmit, formValues, dirty, reset, submitting} = props;
        const [expandedSection, setExpandedSection] = useState(null);
    ...
    

    with only one expanded section, that with its index equal to expandedSection .

    After I extracted the accordion to a separate functional component and moved useState there, the problem was gone.

    0 讨论(0)
  • 2020-12-24 13:04

    As @riscarrott mentioned, put renderField outside of component class .

    But I am still losing focus .. And after testing, I concluded the re-rendering is done because of using curried function (return another function, and not return element . directly) .

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

    Then, if your renderField is a curried function :

    then , don't do

    0 讨论(0)
  • 2020-12-24 13:13

    This can also happen if you have defined styled-components inside your render function. You should define them outside your class. Like this:

    const Row = styled.div`
        justify-content:center;
    `;
    const Card = styled.div`
        width:18rem;
        padding:1rem;
     `;
     class Login extends Component{
    
    0 讨论(0)
  • 2020-12-24 13:19

    What worked for me was refactoring arrowFunction-based Component to class-based Component as the behavior of InputForm components was weird. Every time the value of each input was changed they all rerendered even after splitting each inputType to separated components. There was nothing else left to fix but changing main component to class-based. I guess it may be caused by redux-form itself.

    0 讨论(0)
  • 2020-12-24 13:19

    i have the same problem. i resolved mine by changing the component to Class component and i removed all the css style config from render().

    0 讨论(0)
  • 2020-12-24 13:23

    This happens because you're re-defining renderField as a new component every time you render which means it looks like a new component to React so it'll unmount the original one and re-mounts the new one.

    You'll need to hoist it up:

    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>
        );
      }
    }
    
    ...
    
    0 讨论(0)
提交回复
热议问题