gpt4 book ai didi

reactjs - 使用 redux-form 6.0.0-rc.3 时, Action 创建者未显示在 "this.props"中

转载 作者:行者123 更新时间:2023-12-03 13:33:59 25 4
gpt4 key购买 nike

当我使用 redux-form 5.3.1 时,我能够访问我的 Action 创建者。但由于我需要 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);

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

最佳答案

而不是:

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

用途:

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

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

PS:可能是一种解决方法

关于reactjs - 使用 redux-form 6.0.0-rc.3 时, Action 创建者未显示在 "this.props"中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38297145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com