微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将 Redux-form 连接到 store?

如何解决如何将 Redux-form 连接到 store?

我有一个需要在 handleSubmit 上调用动作创建者的 Redux 表单。

import React from "react";
import "../../styles/formStyles.css";
import { Field,reduxForm } from "redux-form";
import { connect } from "react-redux";
import { onUserUpdate } from "../../actions";

const renderError = ({ error,touched }) => {
  if (error && touched) {
    return <p style={{ padding: "0",margin: "0" }}>{error}</p>;
  }
};

const renderInput = ({ input,label,Meta }) => {
  return (
    <div className="form-Box">
      <input {...input} autoComplete="off" type="text" />
      <label>{label}</label>
      {renderError(Meta)}
    </div>
  );
};

const UpdateProfile = ({ onUserUpdate,handleSubmit }) => {
  const onUpdate = (formValues) => {

    onUserUpdate() ///THIS THIS THE ACTION CREATOR <-------------------------

  };

  return (
    <div className="form-container">
      <h1>Update profile</h1>
      <form onSubmit={handleSubmit(onUpdate)} className="form">
        <Field name="username" component={renderInput} label="New username" />
        <Field name="email" component={renderInput} label="New email" />
        <Field name="password" component={renderInput} label="New password" />
        <Field
          name="confirmPassword"
          component={renderInput}
          label="Confirm new password"
        />
        <button type="submit" className="add">
          Submit
        </button>
      </form>
    </div>
  );
};

const validate = (formValues) => {
  const { password,confirmPassword } = formValues;
  const error = {};

  if (password && password.length < 7)
    error.confirmPassword = "password must be at least 7 characters";

  if (password !== confirmPassword)
    error.confirmPassword = `passwords don't match`;

  if (password && !confirmPassword)
    error.confirmPassword = "confirm new password";

  return error;
};

const connectForm = reduxForm({ form: "update_form",validate })(UpdateProfile);

const mapStatetoProps = (state) => state;

export default connect(mapStatetoProps,{ onUserUpdate })(connectForm);

问题是我无法在不导致应用程序崩溃的情况下调用组件中的动作创建者。我在控制台中收到错误消息:

index.js:1 Warning: Failed prop type: Invalid prop `form` of type `object` supplied to `Form(UpdateProfile)`,expected `string`.

如果我提交表单,则会调用操作创建者并更新商店。但是应用程序崩溃了,我在控制台中收到了这条消息:

Uncaught TypeError: path.lastIndexOf is not a function

在我的 redux 开发工具中,我的表单如下所示: form: object Object: {registeredFields: {...}}

当我只用 reduxForm() 包装组件时,我没有遇到任何问题:

export default reduxForm({ form: "update_form",validate })(UpdateProfile)
///no problems

但我无法访问商店中的操作。如何防止它在提交时崩溃?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。