과거 프로그래밍 자료들/컴퓨터설정&오류
[React] history.push가 오류 나는 경우
평부
2022. 5. 12. 16:29
* 문제 : 회원가입 완료 후 로그인 페이지로 이동 시 오류 발생
* props.history.push로 진행 시 페이지 이동하지 않음
* 해결책 - "따라하며 배우는 노드 리액트 기본" 31강 강의 커뮤니티 질문에서 답을 얻음
문제 해결
수정 전
import React, { useState } from "react";
import { useDispatch } from "react-redux";
import { registerUser } from "../../../_action/user_action";
function RegisterPage(props) {
const dispatch = useDispatch();
const [Email, setEmail] = useState("");
const [Name, setName] = useState("");
const [Password, setPassword] = useState("");
const [ConfirmPassword, setConfirmPassword] = useState("");
const onEmailHandler = (event) => {
setEmail(event.currentTarget.value);
};
const onNameHandler = (event) => {
setName(event.currentTarget.value);
};
const onPasswordHandler = (event) => {
setPassword(event.currentTarget.value);
};
const onConfirmPasswordHandler = (event) => {
setConfirmPassword(event.currentTarget.value);
};
const onSubmitHandler = (event) => {
event.preventDefault();
if (Password !== ConfirmPassword) {
return alert("비밀번호와 비밀번호 확인은 같아야 합니다.");
}
let body = {
email: Email,
password: Password,
name: Name,
};
dispatch(registerUser(body)).then((response) => {
if (response.payload.success) {
alert("회원가입이 완료되었습니다.");
props.history.push("/login") //오류 발생
} else {
alert("회원가입 중 오류가 발생했습니다.");
}
});
};
return (
<div
style={{
display: "flex",
justifyContent: "center",
alignItems: "center",
width: "100%",
height: "100vh",
}}
>
<form
style={{ display: "flex", flexDirection: "column" }}
onSubmit={onSubmitHandler}
>
<label>Email</label>
<input type="email" value={Email} onChange={onEmailHandler} />
<label>Name</label>
<input type="text" value={Name} onChange={onNameHandler} />
<label>Password</label>
<input type="password" value={Password} onChange={onPasswordHandler} />
<label>Confirm Password</label>
<input
type="password"
value={ConfirmPassword}
onChange={onConfirmPasswordHandler}
/>
<br />
<button type="submit">회원 가입</button>
</form>
</div>
);
}
export default RegisterPage;
수정 후(추가1, 2, 3 참고)
import React, { useState } from "react";
import { useDispatch } from "react-redux";
import { registerUser } from "../../../_action/user_action";
import { useNavigate } from "react-router-dom"; //추가1
function RegisterPage(props) {
const dispatch = useDispatch();
const navigate = useNavigate(); //추가2
const [Email, setEmail] = useState("");
const [Name, setName] = useState("");
const [Password, setPassword] = useState("");
const [ConfirmPassword, setConfirmPassword] = useState("");
const onEmailHandler = (event) => {
setEmail(event.currentTarget.value);
};
const onNameHandler = (event) => {
setName(event.currentTarget.value);
};
const onPasswordHandler = (event) => {
setPassword(event.currentTarget.value);
};
const onConfirmPasswordHandler = (event) => {
setConfirmPassword(event.currentTarget.value);
};
const onSubmitHandler = (event) => {
event.preventDefault();
if (Password !== ConfirmPassword) {
return alert("비밀번호와 비밀번호 확인은 같아야 합니다.");
}
let body = {
email: Email,
password: Password,
name: Name,
};
dispatch(registerUser(body)).then((response) => {
if (response.payload.success) {
alert("회원가입이 완료되었습니다.");
navigate("/login"); //추가 3
} else {
alert("회원가입 중 오류가 발생했습니다.");
}
});
};
return (
<div
style={{
display: "flex",
justifyContent: "center",
alignItems: "center",
width: "100%",
height: "100vh",
}}
>
<form
style={{ display: "flex", flexDirection: "column" }}
onSubmit={onSubmitHandler}
>
<label>Email</label>
<input type="email" value={Email} onChange={onEmailHandler} />
<label>Name</label>
<input type="text" value={Name} onChange={onNameHandler} />
<label>Password</label>
<input type="password" value={Password} onChange={onPasswordHandler} />
<label>Confirm Password</label>
<input
type="password"
value={ConfirmPassword}
onChange={onConfirmPasswordHandler}
/>
<br />
<button type="submit">회원 가입</button>
</form>
</div>
);
}
export default RegisterPage;
* 참고하기
* v6로 업그레이드 되면서 달라진 것들
https://velog.io/@soryeongk/ReactRouterDomV6