gpt4 book ai didi

javascript - redux-form onSubmit 刷新页面

转载 作者:数据小太阳 更新时间:2023-10-29 06:14:42 26 4
gpt4 key购买 nike

我是 redux-form 的新手,在处理 onSubmit 时遇到了一个奇怪的问题。

当我完全按照此处的 redux-form 示例设置我的项目时 http://redux-form.com/6.7.0/examples/syncValidation/它按预期工作。我试图根据我的需要扩展这个示例,并确认它在加载表单时按预期工作,如下所示:路由组件 > 表单。

当我尝试在通过路由(路由组件 > 容器组件 > 表单)加载的 react 组件中加载表单时,问题就出现了。当我点击提交时,字段值被添加到地址栏并且表单验证不运行。我已经尝试了我能想到的一切来解决这个问题。如果替换 <Main />,下面提供的代码将正常工作与 <RegisterForm handleSubmit={showResults} />在 index.js 中。有什么我在这里出错的想法吗?

RegisterForm.js:

import React from 'react';
import { Field, reduxForm } from 'redux-form';

const validate = values => {
const errors = {};

if (!values.name) {
errors.name = 'Required';
} else if (values.name.length <= 2) {
errors.username = 'Must be 2 characters or more';
} else if (values.name.length > 50) {
errors.username = 'Must be 50 characters or less';
}

if (!values.email) {
errors.email = 'Required';
} else if (!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z] {2,4}$/i.test(values.email)) {
errors.email = 'Invalid email address';
}

if (!values.password) {
errors.password = 'Required';
} else if (!values.confirm) {
errors.confirm = 'Required';
} else if (values.password !== values.confirm) {
errors.confirm = 'Passwords do not match';
}
return errors;
};
const renderField = ({ input, label, type, id, meta: { touched, error, warning } }) => (
<div>
<label htmlFor={id}>{label}</label>
<div>
<input {...input} id={id} placeholder={label} type={type} />
{touched && ((error && <span>{error}</span>) || (warning && <span>{warning}</span>))}
</div>
</div>
);

const RegisterForm = (props) => {
const { handleSubmit, pristine, reset, submitting } = props
return (
<form onSubmit={handleSubmit}>
<div className="row">
<div className="medium-6 columns medium-centered">
<Field type="text" id="name" name="name" component={renderField} placeholder="name" label="Name" />
</div>
<div className="medium-6 columns medium-centered">
<Field type="text" id="email" name="email" component={renderField} placeholder="email" label="Email" />
</div>
<div className="medium-6 columns medium-centered">
<Field type="password" id="password" name="password" component={renderField} placeholder="password" label="Password" />
</div>
<div className="medium-6 columns medium-centered">
<Field type="password" id="confirm" name="confirm" component={renderField} placeholder="confirm" label="Confirm password" />
</div>
<div className="medium-6 columns medium-centered">
<button type="submit" disabled={submitting}>Submit</button>
</div>
</div>
</form>
);
};

export default reduxForm({
form: 'register', // a unique identifier for this form
validate,
})(RegisterForm);

Index.js(有效):

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { HashRouter as Router, hashHistory } from 'react-router-dom';
const store = require('./store').configure();
import RegisterForm from './RegisterForm.jsx';
import Main from './Main.jsx';

const rootEl = document.getElementById('app');

const showResults = (values) => {
window.alert(`You submitted:\n\n${JSON.stringify(values, null, 2)}`);
}

ReactDOM.render(
<Provider store={store}>
<Router history={hashHistory}>
<div style={{ padding: 15 }}>
<h2>Synchronous Validation</h2>
<RegisterForm handleSubmit={showResults} />
</div>
</Router>
</Provider>,
rootEl,
);

Index.js(不工作):

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { HashRouter as Router, hashHistory } from 'react-router-dom';
const store = require('./store').configure();
import RegisterForm from './RegisterForm.jsx';
import Main from './Main.jsx';

const rootEl = document.getElementById('app');

const showResults = (values) => {
window.alert(`You submitted:\n\n${JSON.stringify(values, null, 2)}`);
}

ReactDOM.render(
<Provider store={store}>
<Router history={hashHistory}>
<div style={{ padding: 15 }}>
<h2>Synchronous Validation</h2>
<Main />
</div>
</Router>
</Provider>,
rootEl,
);

主要.js:

import React, { Component } from 'react';
import RegisterForm from './RegisterForm.jsx';

const showResults = (values) => {
window.alert(`You submitted:\n\n${JSON.stringify(values, null, 2)}`);
};

class Register extends Component {
render() {
return (
<RegisterForm handleSubmit={showResults} />
);
}
}

export default Register;

最佳答案

您应该将提交处理程序传递给 onSubmit 属性,而不是 handleSubmit。它作为 handleSubmit 进入您的表单组件,因此该代码应该没问题。

class Register extends Component {
render() {
return (
//change this
<RegisterForm onSubmit={showResults} />
);
}
}

关于javascript - redux-form onSubmit 刷新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44149685/

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