2016-12-08 4 views
0

私は還元型ライブラリの初心者です。私は、サーバーにデータを更新するためのいくつかのコードを記述しようとしています。サーバーがサポートしている更新API。handleSubmitを使用してフォーム内のデータと余分なデータをredux形式で送信するにはどうすればよいですか?

私はreactjs、redux、およびredux-formを使用しています。 私の簡潔なコードは次のとおりです。

render() { 
    const { handleSubmit, pristine, reset, submitting, updatePost } = this.props; 
    return (
     <div> 
      <h4>Update the Post</h4> 
      <form onSubmit={handleSubmit(updatePost.bind(this))}> 
       <Field name="title" component={this.renderField} type="text" className="form-control" label="Post Title"/> 
       <Field name="description" component={this.renderField} type="textarea" className="form-control" label="Post Description"/> 
       <button type="submit" className="btn btn-primary">Update</button> 
       <Link to='/template'>Back to List</Link> 
      </form> 
     </div> 
    ); 
} 

私は送信ボタンをクリックすると、コードonSubmit={handleSubmit(updatePost.bind(this))}は私の機能updatePostにフォーム内のすべてのデータを送信します。
私の質問です:いくつかのデータ(PostIDのような)を渡したいと思います。どうやってやるの? 私はカンニングを使用したくない(私のフォームに隠れたフィールドのように)。
ありがとうございます。

+0

OK。私は隠されたフィールドを使用することはカンニングではないことに同意します。しかし、隠されたフィールドを使わずに私の問題を解決するにはどうすればいいですか? – user3089480

+0

このハック 'onSubmit = {handleSubmit(updatePost.bind(this))'は何ですか? –

+0

@JyothiBabuAraja。フォームのデータをupdatePost(data)関数に送信します。フォームのすべてのデータを受け取ることができます。 – user3089480

答えて

0

なく、あなたのユースケースがあり、正確にわからしかし、あなたが行うことができますいくつかの方法があります...親コンポーネントのsetStateを使用して

、あなたはinitialValues内部の小道具としてそれを渡すことができます。

class ParentComponent extends Component { 
    updatePost(formValues) { 
     // process data 
     this.setState({ postId: '...' }); 
    } 
    render() { 
     return <FormComponent initialValues={{postId: this.state.postId}} /> 
    } 
} 

ちょうどあなたのreduxFormを初期化するときにtrueにenableReinitializeセットを持っていることを確認してください。

export default reduxForm({ form: 'myFormName', enableReinitialize: true })(FormComponent);

または

使用reduxForm changeアクションクリエータ

import { change } from 'redux-form'; 

class ParentComponent extends Component { 
    updatePost(formValues) { 
     this.props.change('myFormName', 'postId', 'postid_value'); 
    } 
    ... 
} 

export default connect(mapStateToProps, { change })(ParentComponent); 

または支柱自体(<MyForm postId={...} />)としてpostIdを通過し、MyFormをコンポーネント内changeアクションを行います。

関連する問題