Skip to content

자동로그인 시 리디렉션 문제 #569

Answered by woohm402
andhl204 asked this question in Q&A
Discussion options

You must be logged in to vote

useEffect 는 한 차례 렌더가 된 다음 실행됩니다.

console.log('a');

useEffect(() => {
  console.log('b');
}, []);

console.log('c');

위 코드 실행 결과는 a -> b -> c 가 아닌 a -> c -> b 입니다. 즉 스토리지에서 토큰값을 불러오기 전에 렌더가 먼저 진행되고, <Redirect를 먼저 만나게 되고, 로그인으로 리다이렉트가 먼저 됩니다. 뒤이어 useEffect가 토큰값을 받아와서 로그인되었다는 걸 알게 된 후 다시 /students로 리다이렉트되게 됩니다.

#541#542 도 비슷한 고민이 있는 질문이었어서, 참고해보시면 좋을 것 같네요 :)

이를 해결하기 위해서 저는 토큰 상태를 truthy falsy 두 개로 나누지 않고, undefined string null 세 개로 나누는 방식을 선호합니다. undefined일 경우는 첫 렌더라는 뜻이고 아직 토큰 정보를 모른다는 상태로 보고 처리합니다!

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@subir-sh
Comment options

@andhl204
Comment options

andhl204 Oct 9, 2021
Collaborator Author

Answer selected by andhl204
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
React React 관련 내용
3 participants