Skip to content
This repository has been archived by the owner on Jun 15, 2024. It is now read-only.

[BUG] 금융역량테스트 해설 언마운트 안되는 현상 #40

Closed
toothlessdev opened this issue Jan 12, 2024 · 2 comments
Closed
Labels
bug Something isn't working

Comments

@toothlessdev
Copy link
Member

✏️ 버그 발생상황

  1. 금융역량테스트 페이지에서
  2. 정답을 클릭 후, 다음 버튼을 누르면
  3. 해설이 늦게 언마운트 되거나 언마운트 되지 않는 현상 발생

✏️ 스크린샷
image

✏️ 추가 요구사항
정답선택 > 정답여부 표시 > 2초후 정답여부 사라짐 > 해설 보여줌

@toothlessdev toothlessdev added the bug Something isn't working label Jan 12, 2024
@seokiis
Copy link

seokiis commented Jan 13, 2024

setTimeout(() => {
                    setIsCorrect(null);
                    setIsCommentVisible(true);
                }, 2000);
                

이 부분에서 setTImeout cleanUp이 안되는 것 같은데, 다음 페이지 넘어갈 떄 cleanUp 해주면 안되나요?

@toothlessdev toothlessdev pinned this issue Jan 13, 2024
@toothlessdev
Copy link
Member Author

❎ 버그발생코드

        const onChooseOptions = (index: number) => {
            let selectedAnswer = "";
            if (index === 0) selectedAnswer = "O";
            else if (index === 1) selectedAnswer = "X";
            else selectedAnswer = "";s

            return () => {
                if (selectedAnswer === answer) {
                    setIsCorrect(true);
                } else {
                    setIsCorrect(false);
                }
                timeout.current = setTimeout(() => {
                    setIsCorrect(null);
                    setIsCommentVisible(true);
                }, 2000);
            };
        };

        const $options = document.querySelectorAll(`.${questionOptClassName}`);

        for (let index = 0; index < $options.length; index++) {
            $options[index].addEventListener("click", onChooseOptions(index));
        }

✅ 수정된 코드

        const onChooseOptions = (e: Event) => {
            setIsCorrect(e.target?.innerHTML === answer);
            timeout.current = setTimeout(() => {
                setIsCommentVisible(true);
            }, 2000);
        };

        const $options = document.querySelectorAll(`.${questionOptClassName}`);

        for (let index = 0; index < $options.length; index++) {
            $options[index].addEventListener("click", onChooseOptions);
        }

✏️ 버그 원인 : 자바스크립트 함수 & 이벤트리스너 클로져 및 렉시컬 스코프 문제

🔗 참고자료
https://ko.javascript.info/closure
https://poiemaweb.com/js-scope
https://poiemaweb.com/js-closure

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants