2017-02-03 3 views
1

私は単純なフォームを作成し、検証と処理の送信アクションを持つ再利用可能なコンポーネントを構築しようとしています。私はそれを自分で使うことができましたが、私が小道具を注入する機能的なコンポーネントを作ろうとすると、私はこのエラーに悩まされました。redux-formを機能コンポーネントと再利用しようとしています

enter image description here

このクラスには、これはフォーム

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

 
function CustomReduxForm(props) { 
 

 
    class CustomForm extends React.Component { 
 
    render() { 
 
     const { handleSubmit } = this.props; 
 
     return (
 
     <div style={{ margin: '30px' }}> 
 
      <form onSubmit={handleSubmit(props.onSubmit)}> 
 
      {fields.map(myField => renderFieldset(myField))} 
 
      <button className="btn btn-primary" type="submit">Submit</button> 
 
      </form> 
 
     </div> 
 
    ); 
 
    } 
 
    } 
 

 
    const renderInput = field => { 
 
    return (
 
     <div className={`form-group ${field.meta.touched && field.meta.invalid ? 'has-danger' : ''}`}> 
 
      <input 
 
      {...field.input} 
 
      type={field.type} 
 
      className="form-control" 
 
      /> 
 

 
      {field.meta.touched && field.meta.error && <div className="text-help">{field.meta.error}</div>} 
 
     </div> 
 
    ); 
 
    } 
 

 
    const renderFieldset = customField => { 
 
     return (
 
     <div> 
 
      <label htmlFor={customField.name}>{customField.label}</label> 
 
      <Field 
 
      name={customField.name} 
 
      component={renderInput} 
 
      type={customField.type} /> 
 
     </div> 
 
    ); 
 
    } 
 

 
    const validate = values => { 
 
    const errors = {} 
 

 
    props.fields.forEach((customField) => 
 
    { 
 
     if(customField.mandatory && ! values[customField.name]) { 
 
     errors[customField.name] = `You must enter a valid value for ${customField.label}!`; 
 
     } 
 
    }); 
 

 
    return errors 
 
    } 
 

 
    return reduxForm({ 
 
    form: props.formName, 
 
    validate 
 
    })(CustomForm); 
 

 
}; 
 

 
export default CustomReduxForm;
を構築するための成分である形態

import React from 'react'; 
 
import CustomReduxForm from './CustomReduxForm'; 
 

 
class LoginForm extends React.Component { 
 
    getFields() { 
 
    return [ 
 
     { 
 
     name : 'username', 
 
     type : 'text', 
 
     label : 'User', 
 
     mandatory : true 
 
     }, 
 
     { 
 
     name : 'password', 
 
     type : 'password', 
 
     label : 'Password', 
 
     mandatory : true 
 
     } 
 
    ]; 
 
    } 
 

 
    handleFormSubmit(values) { 
 
    console.log(values) 
 
    } 
 

 
    render() { 
 
    return (
 
     <div> 
 
     <div>Test</div> 
 
     <CustomReduxForm 
 
      formName="LoginForm" 
 
      fields={this.getFields()} 
 
      onSubmit={this.handleFormSubmit} 
 
     /> 
 
     </div> 
 
    ); 
 
    } 
 
} 
 

 
export default LoginForm;

を構築するコンポーネントを使用して

私はすでに、作成したフォームをCustomReduxFormにエクスポートする方法を試しましたが、それでも何もしませんでした。

return reduxForm({ 
 
    form: props.formName, 
 
    validate 
 
})(CustomForm); 
 

 
// or 
 

 
const FormWrapped = reduxForm({ 
 
    form: props.formName, 
 
    validate 
 
})(CustomForm); 
 

 
// Non sense, but .. 
 
return FormWrapped; 
 

 
// or 
 
return <FormWrapped />;

ありがとう!

+0

''を返すという最後の考え方で十分です。このアイデアに間違いはありますか?はいの場合、それらは何ですか?また、問題を再現するJSFiddle/JSBin/Webpackbinを作成してください。 – gustavohenke

+0

返事をいただきありがとうございます、今私はこれが動作するようになる、私はjpdelatorreのアドバイスに従った。欠けている部分がもっとあるように見えます。再度、感謝します! –

答えて

2

問題はCustomReduxFormのreturn文です。

正しいものは、それを変数に割り当て、最後のスニペットで行ったようなJSX構文を使用することです。

const WrappedForm = reduxForm(...)(CustomForm); 
return <WrappedForm /> 

あなたは逃した唯一の事はまたWrappedFormCustomReduxFormの小道具を渡すことです。

あなたは今、それはのようなあなたのコード内の他のエラーを修正する問題ですreturn <WrappedForm {...props} />

を使用する必要があります...

その後、あなたは const Fieldset = ...を使用する場合があります

const { fields, handleSubmit } = this.props

代わりのconst renderFieldsetを使用して{fields.map(myFieldset => renderFieldset(myFieldset)

をやっ{fields.map((myFieldset, index) => <Fieldset key={index} />)}

予想通り今では動作するはずです。

0

すべての反応成分には、render()の方法が必要です。

したがって、機能コンポーネントCustomReduxFormにレンダリング方法があることを確認してください。私はあなたが方法レンダリングでリターンをラップする必要があると思い

function render() { 
    return reduxForm({ 
     form: props.formName, 
     validate 
    })(CustomForm); 
} 

しかしあなたがそう別のクラスコンポーネントをラップ機能コンポーネントを作成し、なぜ、私はわかりませんに。 official redux form documentation

見て、どのようにフォームを作成する従来の方法は次のとおりです。

import React from 'react' 
import { Field, reduxForm } from 'redux-form' 

const SimpleForm = (props) => { 
    const { handleSubmit, pristine, reset, submitting } = props 
    return (
    <form onSubmit={handleSubmit}> 
     <div> 
     <label>First Name</label> 
     <div> 
      <Field name="firstName" component="input" type="text" placeholder="First Name"/> 
     </div> 
     </div> 
     <div> 
     <label>Last Name</label> 
     <div> 
      <Field name="lastName" component="input" type="text" placeholder="Last Name"/> 
     </div> 
     </div> 
     <div> 
     <label>Email</label> 
     <div> 
      <Field name="email" component="input" type="email" placeholder="Email"/> 
     </div> 
     </div> 
     <div> 
     <label>Sex</label> 
     <div> 
      <label><Field name="sex" component="input" type="radio" value="male"/> Male</label> 
      <label><Field name="sex" component="input" type="radio" value="female"/> Female</label> 
     </div> 
     </div> 
     <div> 
     <label>Favorite Color</label> 
     <div> 
      <Field name="favoriteColor" component="select"> 
      <option></option> 
      <option value="ff0000">Red</option> 
      <option value="00ff00">Green</option> 
      <option value="0000ff">Blue</option> 
      </Field> 
     </div> 
     </div> 
     <div> 
     <label htmlFor="employed">Employed</label> 
     <div> 
      <Field name="employed" id="employed" component="input" type="checkbox"/> 
     </div> 
     </div> 
     <div> 
     <label>Notes</label> 
     <div> 
      <Field name="notes" component="textarea"/> 
     </div> 
     </div> 
     <div> 
     <button type="submit" disabled={pristine || submitting}>Submit</button> 
     <button type="button" disabled={pristine || submitting} onClick={reset}>Clear Values</button> 
     </div> 
    </form> 
) 
} 

export default reduxForm({ 
    form: 'simple' // a unique identifier for this form 
})(SimpleForm) 
関連する問題