2017-04-25 24 views
0

我正在使用redux-form,我希望组件在提交表单时分派两个动作:第一个动作是从所有表单字段发送数据的动作这是一个内置函数),第二个将用户的状态更新为“注册”。问题是,当我按照下面的方式进行操作时,内置的handleSubmit函数不起作用,只调用“toggleRegistrationStatus”函数。我该如何去调度这些操作,以便数据得到发送并且状态同时得到更新? 我的组件代码:在使用redux-form提交时调度2个动作

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

class RegistrationPage extends React.Component { 

    createAgeOptions(from,to) { 
     var options = [] 
     for(var i=from; i < to; i++) { 
      options.push(<option key={i} value={i}>{i}</option>) 
     } 
     return options 
    } 

    handleRegistration(e) { 
     e.preventDefault() 
     const {handleSubmit, toggleRegistrationStatus, registerUser} = this.props 
     handleSubmit(registerUser) 
     toggleRegistrationStatus() 
    } 

    render() { 
     return (
      <div className="registration"> 
      <form action="" onSubmit={this.handleRegistration.bind(this)} className="registration__form"> 
       <div className="registration__field"> 
       <label htmlFor="name">Name:</label> 
       <Field component="input" id="name" name="name" type="text"/> 
       </div> 
       <div className="registration__field"> 
       <label htmlFor="surname">Surname:</label> 
       <Field name="surname" component="input" id="surname" type="text"/> 
       </div> 
       <div className="registration__field"> 
       <label htmlFor="age">Age:</label> 
       <Field name="select" component="select" name="" id="age"> 
        {this.createAgeOptions(1948,2015)} 
       </Field> 
       </div> 
       <div className="registration__field"> 
       <label htmlFor="email">E-mail:</label> 
       <Field name="email" component="input" id="email" type="email"/> 
       </div> 
       <div className="registration__field"> 
       <label htmlFor="telephone">Telephone number:</label> 
       <Field name="telephone" component="input" id="telephone" type="telephone"/> 
       </div> 
       <button type="submit">Submit!</button> 
      </form> 
      </div> 
     ) 
    } 
} 

export default reduxForm({ 
    form: 'registration' // a unique identifier for this form 
})(RegistrationPage) 

回答

0

你可以尝试删除从表单元素提交,而是把它当点击这样的提交按钮:

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

class RegistrationPage extends React.Component { 

    constructor(props) { 
     super(props); 
     this.handleRegistration = this.handleRegistration.bind(this); 
    } 

    createAgeOptions(from,to) { 
     var options = [] 
     for(var i=from; i < to; i++) { 
      options.push(<option key={i} value={i}>{i}</option>) 
     } 
     return options 
    } 

    handleRegistration(e) { 
     e.preventDefault() 
     const {toggleRegistrationStatus, registerUser} = this.props 
     toggleRegistrationStatus(); 
     registerUser(); 
    } 

    render() { 
     const { handleSubmit } = this.props; 

     return (
      <div className="registration"> 
      <form action="" className="registration__form"> 
       <div className="registration__field"> 
       <label htmlFor="name">Name:</label> 
       <Field component="input" id="name" name="name" type="text"/> 
       </div> 
       <div className="registration__field"> 
       <label htmlFor="surname">Surname:</label> 
       <Field name="surname" component="input" id="surname" type="text"/> 
       </div> 
       <div className="registration__field"> 
       <label htmlFor="age">Age:</label> 
       <Field name="select" component="select" name="" id="age"> 
        {this.createAgeOptions(1948,2015)} 
       </Field> 
       </div> 
       <div className="registration__field"> 
       <label htmlFor="email">E-mail:</label> 
       <Field name="email" component="input" id="email" type="email"/> 
       </div> 
       <div className="registration__field"> 
       <label htmlFor="telephone">Telephone number:</label> 
       <Field name="telephone" component="input" id="telephone" type="telephone"/> 
       </div> 
       <button type="button" onClick={handleSubmit(this.handleRegistration)}>Submit!</button> 
      </form> 
      </div> 
     ) 
    } 
} 

export default reduxForm({ 
    form: 'registration' // a unique identifier for this form 
})(RegistrationPage) 
+0

将这项工作的方式,它应该如果我添加验证?我不希望数据被提交,除非它通过了验证,我一旦将这个问题分类,我想添加它。 – Umbrella

+0

它没有工作,不幸的是 – Umbrella

+0

它应该与验证一起工作,就我而言,验证是以'handleSubmit'函数为界限的,而不是onSubmit属性。那么现在有什么问题? – Shota

相关问题