gpt4 book ai didi

reactjs - 如何将 redux 与 antd 表单验证集成

转载 作者:行者123 更新时间:2023-12-03 14:31:00 29 4
gpt4 key购买 nike

我正在使用react-js、antd和redux验证电子邮件字段,我的问题是为什么当我集成redux(created-form.js)时加载图标在输入中消失,但当我删除redux集成时,加载图标工作正常,我是否在这里遗漏了什么,或者做错了什么?

基本表单.js

 ...

// Constructor

constructor() {
super();

this._validateEmail = _.debounce(this._validateEmail, 1000);
}


// Private method

_validateEmail = (rule, email, callback) => {
const url = 'http://localhost:8000/api/user/isExist';

axios
.post(url, { email })
.then(res => {
if (res.data.isExist) {
callback('Email is already exist');
}

callback();
})
.catch(res => console.log(res));
};


// Render

<Form.Item hasFeedback>
{getFieldDecorator('email', {
rules: [...rules.email, { validator: this._validateEmail }]
})(<Input placeholder="Email" />)}
</Form.Item>

...

创建表单.js

  import { Form } from 'antd';
import AccSetupForm from './base-form';

function mapPropsToFields(props) {
return {
email: Form.createFormField({
value: props.email
}),
password: Form.createFormField({
value: props.password
}),
confirm_pass: Form.createFormField({
value: props.confirm_pass
})
};
}

function onFieldsChange(props, changedField) {
const field = Object.values(changedField)[0];

if (field !== undefined) {
props.updateAccSetup({
[field.name]: field.value
});
}
}

const CreatedForm = Form.create({ mapPropsToFields, onFieldsChange })(
AccSetupForm
);

export default CreatedForm;

index.js

import { connect } from 'react-redux';
import { updateAccSetup } from '../actions';
import CreatedForm from './created-form';

function mapStateToProps(state) {
return {
email: state.getIn(['registration', 'user', 'email']),
password: state.getIn(['registration', 'user', 'password']),
confirm_pass: state.getIn(['registration', 'user', 'confirm_pass'])
};
}

function mapDispatchToProps(dispatch) {
return {
updateAccSetup: userInfo => dispatch(updateAccSetup(userInfo))
};
}

const StepOne = connect(
mapStateToProps,
mapDispatchToProps
)(CreatedForm);

export default StepOne;

最佳答案

我发现了问题,我忘记在form.createFormField内添加...props.username

/* Antd Docu */

mapPropsToFields(props) {
return {
username: Form.createFormField({
...props.username,
value: props.username.value,
}),
};
},

这里有一些引用: https://github.com/ant-design/ant-design/issues/9561 https://ant.design/components/form/#components-form-demo-global-state

关于reactjs - 如何将 redux 与 antd 表单验证集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55530140/

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