Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Action creators not showing in "this.props" with redux-form 6.0.0-rc.3

When I used redux-form 5.3.1 I was able to access my action creators. But as I needed Material-UI, I updated it to 6.0.0-rc.3.

Changes from 5.3.1 to 6.0.0:

  • Removed fields from render():

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

  • Removed errors validation from under inputs:

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

  • Removed fields array from export default:

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

  • Added wrappers for Material-UI components:

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

Code:

1 - console.log(this.props) logs no action creator - should log signupUser function

'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);

EDIT

Changed:

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

To:

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

It worked. Is it the most correct way to fix this?

like image 228
Will Avatar asked Jul 10 '16 22:07

Will


2 Answers

Instead of:

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

Use:

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

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

PS: Might be a work around

like image 174
Will Avatar answered Nov 10 '22 09:11

Will


You can use compose like follow

import { compose } from 'redux'
....
export default compose(
    reduxForm({
      form: 'survey',
    }),
    connect(mapStateToProps, actions)
)(Signup)
like image 37
colorfulberry Avatar answered Nov 10 '22 10:11

colorfulberry