【问题标题】:Dispatching 2 actions upon submitting with redux-form使用 redux-form 提交时调度 2 个操作
【发布时间】:2017-09-22 17:38:17
【问题描述】:

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

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)

【问题讨论】:

    标签: reactjs redux react-redux redux-form


    【解决方案1】:

    您可以尝试从表单元素中删除提交,而是在单击提交按钮时调用它,如下所示:

    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)
    

    【讨论】:

    • 如果我添加验证,这是否会正常工作?除非通过验证,否则我不希望提交数据,我想在解决此问题后立即添加。
    • 它应该与验证一起使用,就我而言,验证与handleSubmit 函数绑定,而不是与 onSubmit 属性绑定。那么现在有什么问题呢?
    • 问题是内置的handleSubmit函数仍然没有被调用,它在提交时只调用了“toggleRegistrationStatus”函数。没有任何变化。
    • 非常感谢您的更新,现在可以使用了!是的,我以为你的意思是我应该在表单和按钮上添加事件处理程序,这就是它不起作用的原因。顺便说一句,在这种情况下,不需要阻止默认行为,因为它默认被 handleSubmit 函数阻止。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-31
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    • 2018-08-16
    相关资源
    最近更新 更多