redux-form的onSubmit会刷新页面。

6

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

当我按照redux-form示例在这里设置我的项目 http://redux-form.com/6.7.0/examples/syncValidation/ 时,就能正常工作。我尝试根据我的需求扩展此示例,并确认它在以这种方式加载表单时能够按预期工作:路由组件 > 表单。

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

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(works):

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

Main.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;

1
<Form onSubmit={this.props.handleSubmit(mySubmitHandler)}> 对我很有用。 - totymedli
1个回答

8
你应该将提交处理程序传递给onSubmit属性,而不是handleSubmit。它以handleSubmit的形式进入您的表单组件,因此该代码应该没问题。
class Register extends Component {
    render() {
        return (
            //change this
            <RegisterForm onSubmit={showResults} />
        );
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接