问题
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?
回答1:
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
回答2:
You can use compose like follow
import { compose } from 'redux'
....
export default compose(
reduxForm({
form: 'survey',
}),
connect(mapStateToProps, actions)
)(Signup)
回答3:
To get those values into your redux-form-decorated component, you will need to connect() to the Redux state yourself and map from the data reducer to the initialValues prop.
http://redux-form.com/6.0.0-alpha.4/examples/initializeFromState/
回答4:
You can use constructor to bind method with your existing props. you do not need to pass actions and initial state.
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);
来源:https://stackoverflow.com/questions/38297145/action-creators-not-showing-in-this-props-with-redux-form-6-0-0-rc-3