HOC auth.js에 관한 오류 발생 시 참고 사항
801
10 asked
강사님 강의 내용과 똑같은 코드를 작성했으나 오류가 계속 생성됬는데 어찌저찌 풀어서 참고하실 분들을 위해 남깁니다
제가 오류가 발생한 상황(hoc/auth.js)
[강사님과 같은 코드를 작성 후 서버/클라이언트 실행 시 빈 화면이 나옴]
(오류 내용 : unctions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render. Or maybe you meant to call this function rather than return it.)
- 해결책 : AuthenticationCheck 리턴 시 함수로 리턴하는 것이 아닌 JSX 컴포넌트로 리턴하라고 합니다
(참고 : https://www.inflearn.com/questions/357383)
- 실제로 auth.js의 마지막 부분인 을 return AuthenticationCheck -> return <AuthenticationCheck />로 변경 시 정상적으로 작동함을 확인할 수 있었습니다.
[hoc/auth.js 코드]
import React, { useEffect } from "react";
import { useDispatch } from "react-redux";
import { auth } from "../_action/user_action";
import { useNavigate } from "react-router-dom";
export default function (SpecificComponent, option, adminRoute = null) {
//option : null = anyone, true = login user only, false = logout user only
function AuthenticationCheck(props) {
const dispatch = useDispatch();
const navigate = useNavigate();
useEffect(() => {
dispatch(auth()).then((response) => {
console.log("auth? ", response);
if (!response.payload.isAuth) {
// login yet
if (option) {
navigate("/login");
}
} else {
// login
if (adminRoute && !response.payload.isAdmin) {
navigate("/");
} else {
if (option === false) {
navigate("/");
}
}
}
});
}, []);
return (
<SpecificComponent /> // component return이 없으면 React 실행이 안됨.
);
}
return <AuthenticationCheck />;
}
깃 이메일이랑 비번이 필요하다고 하네요
0
32
1
404 에러
0
101
1
34강 인증 체크에서 element 사용 때문에 에러나시는 분들 이렇게 하심 됩니다.
0
118
1
로그인, 로그아웃, 토근 작동 안 함
0
237
0
9강 오류 어떻게 해결하나요?
0
192
1
localhost 에서 연결을 거부했습니다.
0
1921
4
포스트맨에서 true가 안떠요
0
150
1
왜 안되나요
0
128
1
몽고db 연결 오류가 납니다 위에껀 입력한 코드, 아래껀 터미널이에요
0
242
1
로그아웃 401 에러(Unauthorized)
0
501
2
암호가 해싱되지 않고 입력값 그대로 db에 저장되는 문제
0
148
1
7강중에서
0
162
2
User.findByToken is not a function
0
210
1
루트 디렉토리
0
268
1
useState
0
560
1
프록시 잘 설정했는데도 404 오류 뜨는 분들
5
874
6
webpack 관련 에러 질문
0
218
1
리액트 관련 질문
0
271
1
14강 로그아웃 안됨
0
315
1
mongoDB 데이터 확인하는 법
0
407
1
postman 에러
0
290
1
선생님 리덕스를 사용하면 어떠한 부분이 좋은지 알 수 있을까요?
0
232
1
다음과 같은 에러들이 발생합니다.
0
270
1
14강 로그아웃 기능 구현시 postman에서 Cannot POST 오류가 뜹니다.
0
378
1

