Redux Form HandleSubmit数据为空

时间:2016-04-13 00:50:25

标签: reactjs redux redux-form

问题

表单的数据在提交时未传递给handleSubmit()函数。我做错了什么?

表格

import React from 'react'
import { reduxForm } from 'redux-form'
export const fields = ['email', 'password']

const validate = (values) => {
  const errors = {}
  return errors
}

export class SignUp extends React.Component {
  props: Props;

  render() {
    const {
      fields: { email, password },
      handleSubmit
    } = this.props

    return (
      <form onSubmit={handleSubmit}>
        <div>
          <label>Email</label>
          <div>
            <input type="email" placeholder="you@gmail.com" {...email}/>
          </div>
        </div>
        <div>
          <label>Password</label>
          <div>
            <input type="password" placeholder="Password" {...password}/>
          </div>
        </div>
        <button type="submit" class="btn btn-primary">Submit</button>
      </form>
    )
  }
}

SignUp = reduxForm({
  form: 'SignUp',
  fields,
  validate
})(SignUp)

export default SignUp

表单的父级

export class SignUpView extends React.Component {
  props: Props;

  constructor(props) {
    super(props)

    this.onSignUp = this.onSignUp.bind(this)
  }

  onSignUp(data) {
    console.log(data) <-- data is just an empty Object {}
    this.props.signUp({
      email: email,
      password: password
    });
  }

  render() {
    var fields = {
      email: '',
      password: ''
    }

    return (
      <div>
        <h1>Sign Up Form</h1>
        <SignUpForm
          fields={fields}
          onSubmit={this.onSignUp}
        />
      </div>
    )
  }
}

2 个答案:

答案 0 :(得分:0)

您正在尝试使用this.props中的handleSubmit,但是您正在传递onSubmit

您可以执行以下操作重新映射它:

const {
  fields: { email, password },
  onSubmit: handleSubmit
} = this.props

这是(跳过字段位)的简写:

const handleSubmit = this.props.onSubmit;

答案 1 :(得分:0)

你不应该传入任何字段prop,因为它们是在表单组件中定义的(正确)。

<SignUpForm
  fields={fields} // <----- REMOVE THIS
  onSubmit={this.onSignUp}
/>

您尝试做的是将初始值设置为''。如果你想这样做,你可以这样做:

<SignUpForm
  initialValues={{ email: '', password: '' }}
  onSubmit={this.onSignUp}
/>

但这不是必需的,因为''已经是v5.x中的默认值。