2017-02-20 4 views
1

私のreduxフォームのtouchプロパティにアクセスしようとしていますが、何らかの理由でフィールドpropsを印刷するとオブジェクトの代わりに値のみが表示されます。私は何が欠けていますか?なぜreduxフォームはオブジェクトではなく文字列を返しますか?

import { reduxForm, Field } from 'redux-form'; 

render() { 
    const { fields: { email, phone }, handleSubmit } = this.props; 
    console.log(email) //prints just the value "email" instead of the field object with the touched method, etc. When I do console.log(email.touched) I get undefined error. 
    return (
    <form onSubmit={handleSubmit(this.onSubmit)}> 
     <Field name="email" component="input" type="email" { ...email } /> 
     <Field name="phone" component="input" type="number" { ...phone } />  
    </form> 
); 


} 


export default ReduxFormTest = reduxForm({ 
    form: 'uniqueForm', 
    fields: ['email', 'phone'] 
})(TestClass); 

答えて

2

還元型がv5からv6に急激に変化しました。以前は、タッチされたフィールドにアクセスする必要があるものと同様のことを行うことができました。フィールドにエラーがあるかどうかを似たようにするには、redux-formのFieldコンポーネントに渡す独自のコンポーネントを作成する必要があります。

カスタムコンポーネント

const CustomComponent = function(field) { 
    return (
    <div> 
     <input 
     type={field.type} 
     {...field.input} 
     /> 
     {field.meta.touched && field.meta.error && <div className='error'>{field.meta.error}</div>} 
    </div> 
) 
} 

そして、フィールドコンポーネント

<Field name="my-prop" component={CustomComponent} /> 

でそれを使用しても、移行guideを見て、これが役に立てば幸い!

+0

これは機能しますが、入力に1文字入力すると非アクティブになり、入力を続行するにはもう一度クリックする必要があります。 – joethemow

+0

私はあなたがfield.meta.touched行を編集したのを見ましたが、私はまだそれを追加していないので、問題はそれにありません。 – joethemow

+0

上記の問題は、入力しようとする最初のフィールドでのみ発生します。 – joethemow

0

v5の構文とv6の構文を混同しています。 v6では、あなたの装飾されたフォームコンポーネントは、もはやthis.props.fieldsを渡されません。 @ tyler-iguchiさんのように、移行ガイドを再度読んでください。

関連する問題