【发布时间】:2021-04-13 08:21:07
【问题描述】:
这是我在 REACTJS 中的用户注册代码,每当我点击注册按钮时,redux for request 都会工作,然后它会转到 request_fail 而不是成功。在此之后,当我看到我的 mongo 集合时,数据就被完美地存储了。我在邮递员中检查了 API,所以它工作正常,所以我尽力解决错误但没有成功。 如果有人找到解决方案,那将是一个很大的帮助
import React, { useState, useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import {
Container, Col, Form,
FormGroup, Label, Input,
Button,
} from 'reactstrap';
import { signupuserAction } from '../../redux/actions/users/userActions';
const Signup = ({ history }) => {
const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [bio, setBio] = useState('');
const [jobtitle, setJobtitle] = useState('');
const [tech, setTech] = useState('');
const dispatch = useDispatch();
//getting user login from store
const state = useSelector(state => {
return state.userLogin;
});
const { loading, userInfo, error } = state
//Redirecting if user is login/authenticated
useEffect(() => {
if (userInfo) {
history.push('/');
}
}, [userInfo])
const submitHandler = e => {
e.preventDefault();
//dispatching action
dispatch(signupuserAction(name, email, password, bio, jobtitle, tech));
}
return (
<Container className="signup">
<h2>Sign Up</h2>
<Form className="form" onSubmit={submitHandler}>
<Col>
<FormGroup>
<Label for="name">Name*</Label>
<Input
type="text"
name="name"
id="name"
placeholder="Enter Your Full Name"
onChange={e => setName(e.target.value)}
value={name}
/>
</FormGroup>
</Col>
<Col>
<FormGroup>
<Label for="email">Email*</Label>
<Input
type="email"
name="email"
id="email"
placeholder="myemail@email.com"
onChange={e => setEmail(e.target.value)}
value={email}
/>
</FormGroup>
</Col>
<Col>
<FormGroup>
<Label for="password">Password*</Label>
<Input
type="password"
name="password"
id="password"
placeholder="********"
onChange={e => setPassword(e.target.value)}
value={password}
/>
</FormGroup>
</Col>
<Col>
<FormGroup>
<Label>Bio*</Label>
<Input
type="text"
name="bio"
id="bio"
placeholder="Enter a breif Introduction about Yourself"
onChange={e => setBio(e.target.value)}
value={bio}
/>
</FormGroup>
</Col>
<Col>
<FormGroup>
<Label>Job Title*</Label>
<Input
type="text"
name="jobtitle"
id="jobtitle"
placeholder="Sotware Developer"
onChange={e => setJobtitle(e.target.value)}
value={jobtitle}
/>
</FormGroup>
</Col>
<Col>
<FormGroup>
<Label>Technology*</Label>
<Input
type="text"
name="tech"
id="tech"
placeholder="ReactJS, NodeJS, Python "
onChange={e => setTech(e.target.value)}
value={tech}
/>
</FormGroup>
</Col>
<Button color="primary" className="btn-submit">Submit</Button>
</Form>
</Container>
);
}
export default Signup;
【问题讨论】:
-
你能分享一下你的
userLoginreducer 函数以及你如何组合你的 reducer 来为你的 redux 提供者创建你的 store 对象吗? -
这里是 gihtub 存储库
-
@DrewReese 上面是链接,你能查一下吗
-
我在您的操作代码中发现了一个可能的错误(在下面的答案中解决),但无法追踪导致
state的路径(state.userLogin@ 987654325@Signup` 组件。您确定这是您访问未定义的loading的地方吗?
标签: reactjs react-redux