大家好,我想做一个React-Toast的通知,用于捕捉,但它不工作。
目前我的函数是这样的
const naviagte = useNavigate();
const dispatch = useDispatch();
const [inputs, setInputs] = useState({
name: "",
email: "",
password: "",
gender: "",
type: "",
id: "",
birthday: "",
});
const [isSignup, setIsSignup] = useState(false);
const handleChange = (e) => {
setInputs((prevState) => ({
...prevState,
[e.target.name]: e.target.value,
}));
};
const sendRequest = async (type = "login") => {
const res = await axios
.post(`http://localhost:5000/api/user/${type}`, {
name: inputs.name,
email: inputs.email,
password: inputs.password,
gender: inputs.gender,
birthday: inputs.birthday,
type: inputs.type,
id: inputs.id,
})
.then((res) => {
if (res.status === 200) {
ToastN(res.data.message, "success");
}
})
.then(() =>
dispatch(
login({
name: inputs.name,
email: inputs.email,
password: inputs.password,
gender: inputs.gender,
birthday: inputs.birthday,
type: inputs.type,
id: inputs.id,
isloggedIN: true,
})
)
)
.then(() => naviagte("/Home"))
.catch((res) => {
if (res.status === 400) {
ToastN(res.message, "warning");
}
});
const data = await JSON.stringify(res.data);
return data;
};
const handleSubmit = (e) => {
e.preventDefault();
if (isSignup) {
sendRequest("signup")
.then(() =>
dispatch(
login({
name: inputs.name,
email: inputs.email,
password: inputs.password,
gender: inputs.gender,
birthday: inputs.birthday,
type: inputs.type,
id: inputs.id,
isloggedIN: true,
})
)
)
.then(() => naviagte("/Home"));
} else {
sendRequest("login");
}
};
成功是正常的,但不能进行错误调用,不知道哪里出了问题,尝试了所有方法,不知道该怎么做
或者我应该为这个添加Formick吗?