当我尝试将表单组件连接到redux时,出现无限循环。 如果它不会令人沮丧,那将很有趣,因为该代码在今天早些时候可以正常工作,而现在在我还原1次提交后它不再起作用。
代码几乎相同,connect()()有效,我可以使用signUserUp函数作为道具来使用。为了以防万一,我删除了node_modules并对其进行了包锁定,然后重新安装,一切相同。
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的示例,至少在我尝试访问注册页面时,才能看到错误。
答案 0 :(得分:1)
问题在于您如何向signupActions
应用reducer
(默认导出为action creator
而不是connect
)。
工作示例(包括以下注释):https://codesandbox.io/s/4rook7ryv4
注释:
将containers
与components
分开:https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0
分隔actions
,reducers
和types
。
我已经对您的应用程序进行了重组,以使其更易于理解/阅读。我强烈建议您使用下面列出的这种流程和结构(或者,如果您想使组件/视图更具模块化,请将与该组件/视图相关的所有内容捆绑到一个文件夹中,如果这样做会更好。您正在一个大型团队中工作。)
├── 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