react-redux 连接 Formik - 超出最大调用堆栈大小

react-redux connect with Formik - Maximum call stack size exceeded

当我尝试将我的表单组件连接到 redux 时出现无限循环。 如果它不令人沮丧,那会很有趣,因为代码在今天早些时候工作,但现在在我恢复 1 次提交后它不工作了。

代码几乎相同,并且 connect()() 有效,我有 signUserUp 函数可作为道具使用。我删除了 node_modules 和 package-lock 以防万一,然后重新安装,结果是一样的。

Signup.js

import { connect } from "react-redux";
import signupActions from "../modules/signup";

function Signup() {
  return <BasicForm />
}

const BasicForm = () => (
  <Formik
    initialValues={{ email: "", password: "", confirmPassword: "" }}
    validationSchema={signupSchema}
    onSubmit={(values, actions) => {
      console.log("form data: ", values);
    }}
    render={({ values, errors, touched, isSubmitting, handleChange, handleBlur, handleSubmit }) => (
      <Container>
        <Form onSubmit={handleSubmit}>
          <Form.Input
            type="text"
            placeholder="Email"
            onChange={handleChange}
            onBlur={handleBlur}
            value={values.email}
            name="email"
          />
          {errors.email && touched.email && <div>{errors.email}</div>}

          <Form.Input
            type="password"
            placeholder="Password"
            onChange={handleChange}
            onBlur={handleBlur}
            value={values.password}
            name="password"
          />
          {errors.password && touched.password && <div>{errors.password}</div>}

          <Form.Input
            type="password"
            placeholder="Confirm Password"
            onChange={handleChange}
            onBlur={handleBlur}
            value={values.confirmPassword}
            name="confirmPassword"
          />
          {errors.confirmPassword && touched.confirmPassword && <div>{errors.confirmPassword}</div>}

          <Button primary type="submit" onSubmit={handleSubmit} disabled={isSubmitting}>
            Submit
          </Button>
        </Form>
      </Container>
    )}
  />
);

export default connect(
  null,
  signupActions
)(Signup);

当我添加连接时,这里明显出现错误...

这是带有 redux 东西的单文件模块 signup.js

import api from '../utils/api';

const SIGN_UP = 'signup/SIGN_UP';

const initialState = {
  isAuthenticated: false,
  jwt: '',
  authError: '',
  formErrors: [],
};

export default (state = initialState, action = {}) => {
  switch (action.type) {
    case SIGN_UP:
      return { ...state, isAuthenticated: true, jwt: action.payload, authError: '', formErrors: [] };
    default:
      return state;
  }
};

export const signup = token => ({
  type: SIGN_UP,
  payload: token,
});

export const signUserUp = credentials => {
  return dispatch => api.post('auth/signup', credentials).then(token => dispatch(signup(token)));
};

rootReducer.js 中,我像这样导入 signupReducer

import signupReducer from './modules/signup';

export default combineReducers({
  signup: signupReducer,
});

这里是 codesandbox 示例,至少在我尝试访问注册页面时可以看到错误。

问题是您如何将 signupActions(默认导出为 reducer 而不是 action creator)应用到 connect

工作示例(包括以下注释):https://codesandbox.io/s/4rook7ryv4


备注:

将您的 containerscomponents 分开:https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0

将您的 actionsreducerstypes 分开。

我已经重组了您的应用程序,使其更易于 understand/read。我强烈建议您使用下面列出的流程和结构(或者,如果您希望 components/views 更加模块化,请将与 component/view 相关的所有内容捆绑到一个文件夹中——这个如果你在一个大团队中工作会更好。

├── build
|   ├── css
|   |   ├── main.[contenthash:8].css
|   |   └── main.[contenthash:8].css.map
|   ├── js
|   |   ├── main.[hash].js
|   |   └── main.[hash].js.map
|   ├── media
|   |   └── [hash].[ext]
|   └── favicon.ico
|   └── index.html
|
├── public
|   ├── favicon.ico
|   └── index.html
|
├── src
|   ├── actions
|   ├── components
|   ├── containers
|   ├── images
|   ├── reducers
|   ├── routes
|   ├── store
|   ├── styles
|   ├── tests
|   ├── types
|   ├── utils
|   ├── views
|   ├── index.js
|   ├── serviceWorker.js
|   └── setupTests.js
|
├── .eslintrc
├── .gitignore
├── README.md
├── package.lock.json
└── package.json