-
글쓴이글
-
2021년 12월 10일 20:02 #21725
코딩초보참가자
임폴트 useHistory 를 한후 let history = useHistory();를 선언하고 회원가입이 완료되면 다른페이지로 넘어가게 하고싶어서histort.push 를 사용하였는데 페이지 이동이 제대로 되지않습니다. let history = useHistory(); 사용법이 잘못되었나요?
2021년 12월 10일 22:03 #21737
코딩초보참가자회원가입 전체코드입니다
import React, { useState } from 'react';
import './Register.css'
import axios from 'axios';
import { useHistory } from 'react-router-dom'function Register() {
let [inputData, inputData변경] = useState('');return (
<div ><section className="box">
<h1>회원가입</h1>
<div className="rgad">
<div className='malf'><form className='cu' onSubmit={Registerdata}>
<div className="row gtr-uniform gtr-50"><div>
<h5>이름</h5>
<input type="text" name="name" placeholder="이름" />
</div>
<div>
<h5>이메일</h5><input type="email" name="email" placeholder="Email" onChange={(e) => {
inputData변경(e.target.value)}} />
</div>
<div >
<input type="button" value="이메일 중복확인" onClick={
() => {
emailch(inputData)}} />
</div>
</div>
<div className="row gtr-uniform gtr-50">
<div>
<h5>비밀번호</h5>
<input type="password" name="pw"
placeholder="password를 입력하세요" />
</div>
<div>
<h5>비밀번호 확인</h5>
<input type="password" name="pwch"
placeholder="비밀번호를 한번더 입력해주세요" /></div>
</div>
<div >
<input type="submit" value="회원가입" />
</div>
</form>
</div>
</div>
</section></div>
);
}function Registerdata(e) {
let history = useHistory();
history.push('/cart')
e.preventDefault();const { email, pw, name, pwch } = e.target.elements;
if (!(name.value && email.value && pw.value && pwch.value)) {
return alert("모두 작성하시고 다시 눌러주세요.")
}
if (!(localStorage.getItem("emailch"))) {
return alert('이메일 중복을 확인하세요')
}
if (!(e.target.elements.pw.value === e.target.elements.pwch.value)) {
return alert('비밀번호를 다시 확인해주세요')
}
console.log(email.value)
const newUser = {
email: email.value,
password: pw.value,
name: name.value
};axios.post("http://localhost:5000/user/register", newUser)
.then((response) => {
alert("회원가입 성공");
//localStorage.removeItem("emailch");})
.catch((err) => {
if (!err.response) {
alert(err);
}
else {
alert(err.response.data)
}});
}function emailch(inputData) {
var mail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
let email = inputData;
if (email == '' || !mail.test(email)) {
return alert("올바른 이메일 주소를 입력하세요")
}
axios.post('http://localhost:5000/user/register/emailch', {
email: email,})
.then(response => {alert("이 이메일은 사용이 가능합니다.")
localStorage.setItem("emailch", response.data.token);}).catch(err => {
alert("이미 사용 중인 이메일 입니다.");})
}export default Register;
Registerdata 함수 안에 let history = useHistory(); 만 추가되면
이 오류창이 빠르게 나타났다가 사라집니다2021년 12월 10일 22:06 #21739
codingapple키 마스터콘솔창에 아무것도 안뜨면 서버에서 응답을 안해준게 아닐까요
그래서 then, catch 내부 코드가 실행안될 수도 있습니다
2021년 12월 10일 22:11 #21740
codingapple키 마스터invalid hook call 어쩌구는 문법틀린게 아니면 80프로 확률로 라이브러리 재설치하면 해결됩니다
아니면 처음부터 프로젝트 새로 만들고 코드를 거기로 옮겨봅시다
-
글쓴이글
- 답변은 로그인 후 가능합니다.