行动创作者没有出现在" this.props"使用redux-form 6.0.0-rc.3

时间:2016-07-10 22:53:03

标签: reactjs react-redux material-ui redux-form

当我使用redux-form 5.3.1时,我能够访问我的动作创建者。但是当我需要Material-UI时,我将其更新为6.0.0-rc.3。

从5.3.1更改为6.0.0:

  • 从render()中删除字段:

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

  • 从输入下删除了错误验证:

{email.touched && email.error && <div className="error">{email.error}</div>}

  • 从导出默认值中删除了字段数组:

export default reduxForm({ form: 'signup', fields: ['email', 'password', 'passwordConfirm'], validate }, mapStateToProps, actions)(Signup);

  • 为Material-UI组件添加了包装器:

import { renderTextField, renderCheckbox, renderSelectField, renderDatePicker } from '../material-ui-wrapper';

代码:

1 - console.log(this.props)不记录任何动作创建者 - 应该记录signupUser函数

'use strict';
import React, { Component } from 'react';
import { reduxForm, Field } from 'redux-form';
import * as actions from '../../actions';

import { renderTextField } from '../material-ui-wrapper';

import {Card, CardActions, CardHeader, CardText} from 'material-ui/Card';
import FlatButton from 'material-ui/FlatButton';

class Signup extends Component {
  handleFormSubmit(formProps) {
    console.log(this.props);
    this.props.signupUser(formProps);
  }

  renderAlert() {
    if (this.props.errorMessage) {
      return (
        <div className="error">
          {this.props.errorMessage}
        </div>
      );
    }
  }

  render() {
    const { handleSubmit, pristine, submitting } = this.props;
    return (
      <form id="form" onSubmit={handleSubmit(this.handleFormSubmit.bind(this))}>
        <Card>
          <CardHeader title="Cadastre-se"/>
          <CardText>
            <Field name="email" component={renderTextField} label={"Email"} fieldType="text"/>
            <Field name="password" component={renderTextField} label={"Senha"} fieldType="password"/>
            <Field name="passwordConfirm" component={renderTextField} label={"Confirmação de senha"} fieldType="password"/>
            {this.renderAlert()}
          </CardText>
          <CardActions>
            <FlatButton type="submit" label="Criar!"/>
          </CardActions>
        </Card>
      </form>
    );
  }
}

function validate(formProps) {
  const errors = {};

  if (!formProps.email || !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(formProps.email)) {
    errors.email = 'Por favor, informe um email válido';
  }

  if (formProps.password !== formProps.passwordConfirm) {
    errors.password = 'Senhas devem ser iguais.';
  }

  if (!formProps.password) {
    errors.password = 'Por favor, informe uma senha.';
  }

  if (!formProps.passwordConfirm) {
    errors.passwordConfirm = 'Por favor, confirme a sua senha.';
  }

  return errors;
}

function mapStateToProps(state) {
  return { errorMessage: state.auth.error };
}

export default reduxForm({
  form: 'signup',
  validate
}, mapStateToProps, actions)(Signup);

修改

更改:

export default reduxForm({ form: 'signup', validate }, mapStateToProps, actions)(Signup);

要:

Signup = reduxForm({ form: 'signup', validate })(Signup); export default Signup = connect(mapStateToProps, actions)(Signup);

工作。这是解决这个问题最正确的方法吗?

4 个答案:

答案 0 :(得分:8)

而不是:

export default reduxForm({
  form: 'signup',
  validate
}, mapStateToProps, actions)(Signup);

使用:

Signup = reduxForm({
form: 'signup',
validate})(Signup);

export default Signup = connect(mapStateToProps, actions)(Signup);

PS:可能是一个解决方法

答案 1 :(得分:3)

您可以使用如下的撰写

import { compose } from 'redux'
....
export default compose(
    reduxForm({
      form: 'survey',
    }),
    connect(mapStateToProps, actions)
)(Signup)

答案 2 :(得分:0)

  

要将这些值放入redux-form-decorated组件中,您将会这样做   需要自己连接()到Redux状态并从数据映射   reducer到initialValues prop。

http://redux-form.com/6.0.0-alpha.4/examples/initializeFromState/

答案 3 :(得分:0)

  

您可以使用构造函数将方法与现有道具绑定。   你不需要传递动作和初始状态。

class Signup extends Component {

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

   handleSubmit(formProps) {
     console.log(this.props);
     this.props.signupUser(formProps);
  }
  render() {

  const { handleSubmit, pristine, submitting } = this.props;

  return (

  <form id="form" onSubmit={this.handleFormSubmit}>
    <Card>
      <CardHeader title="Cadastre-se"/>
      <CardText>
        <Field name="email" component={renderTextField} label={"Email"} fieldType="text"/>
        <Field name="password" component={renderTextField} label={"Senha"} fieldType="password"/>
        <Field name="passwordConfirm" component={renderTextField} label={"Confirmação de senha"} fieldType="password"/>
       </CardText>
        <CardActions>
          <FlatButton type="submit" label="Criar!"/>
        </CardActions>
      </Card>
    </form>
   );
  }
 }
}
Signup = reduxForm({
 form: 'signup',
 validate})(Signup);

export default Signup = connect()(Signup);