簡體   English   中英

redux-form onSubmit刷新頁面

[英]redux-form onSubmit refreshes page

我是redux-form的新手,在處理onSubmit時遇到了一個奇怪的問題。

當我按照http://redux-form.com/6.7.0/examples/syncValidation/中的redux-form示例完全設置項目時,它可以按預期工作。 我已嘗試根據自己的需要擴展此示例,並確認它在加載表單時可以按預期工作:route component> form。

當我嘗試在通過路由加載的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(有效):

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;

您應該將提交處理程序傳遞給onSubmit道具,而不是handleSubmit。 它作為handleSubmit進入表單組件,因此代碼應該沒問題。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM