简体   繁体   English

redux-form v6不能在Field属性中使用renderField

[英]redux-form v6 can't use renderField inside Field property

I just try redux-form v6 RC 4, i think it nicer than v5. 我只是尝试使用redux形式的v6 RC 4,我认为它比v5更好。 but i get stucked at somewhere on my code. 但是我被卡在代码的某个地方。 my component didn't work with renderField, because of that my "submit" and "clear value" button can't be enable. 我的组件无法与renderField一起使用,因为无法启用我的“提交”和“清除值”按钮。 can someone check out my code, what's wrong in it. 有人可以检出我的代码,其中有什么问题。 thanks. 谢谢。

 import React from 'react'; import {reduxForm, Field} from 'redux-form'; import * as actions from '../actions'; import {browserHistory} from 'react-router'; const validate = values => { const errors = {}; if (!values.email) { errors.email = 'Required' } else if (!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[AZ]{2,4}$/i.test(values.email)) { errors.email = 'Invalid email address' } if (!values.password) { errors.password = 'Required' } return errors }; const renderField = (props) => ( <div> <label>{props.placeholder}</label> <div> <input {...props}/> {props.touched && props.error && <span>{props.error}</span>} </div> </div> ); const Loginv6 = (props) => { const {handleSubmit, pristine, reset, submitting} = props; return ( <div className="row"> <div className="col-md-6"> <form onSubmit={handleSubmit}> <div className="form-group"> <Field name = "email" type="text" component = {renderField} className="form-control" placeholder="Email" /> </div> <div className="form-group"> <Field name = "password" type="password" component = {renderField} className="form-control" placeholder="Password" /> </div> <div> <button type="submit" className="btn btn-primary" disabled={pristine||submitting}> Login </button> <button type="button" className="btn btn-primary" disabled={pristine || submitting} onClick={reset}> Clear Values </button> </div> </form> </div> </div> ) }; function mapStateToProps(state) { return { errorMessage: state.auth.error, authenticated:state.auth.authenticated } } export default reduxForm({ form:'Loginv6', validate },mapStateToProps,actions)(Loginv6); 

if i change component = {renderField} to component = input everything is working fine. 如果我将component = {renderField}更改为component = input一切工作正常。 i still don't know why. 我仍然不知道为什么。 i already follow the documentation in redux form v6 RC4 我已经按照Redux形式v6 RC4遵循文档

Just spent 4 hours trying to figure this one out also. 仅仅花了4个小时就试图弄清楚这一点。 Finally got it. 终于明白了。

Change renderField to: 将renderField更改为:

const renderField = (props) => (
    <div>
        <label>{props.placeholder}</label>
        <div>
            <input {...props.input}/>
            {props.meta.touched && props.meta.error && <span> {props.meta.error} </span>}
        </div>
    </div>
);

They changed the props that get passed through to your Field's component in order to get around the new warnings react throws for invalid attributes on DOM elements. 他们更改了传递到Field组件的道具,以绕过针对DOM元素上无效属性的新警告反应。 They need to fix the examples in the docs to reflect this. 他们需要修复文档中的示例以反映这一点。

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

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