bugfix: openchat 내용 업데이트 #303
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull requests
✅ 작업한 내용
*문제 원인
오픈 채팅에서 실시간 메시지가 표시되지 않던 원인은 WebSocket 메시지 핸들러가 오래된 클로저를 참조하고 있었기 때문.
handleWebSocketMessage 함수가 일반 함수로 정의되어 매 렌더링마다 새로 생성되었지만, useEffect는 초기 마운트 시에만 실행되어 초기 렌더링 때의 함수만 WebSocket 핸들러로 등록됨. 이 함수는 초기 상태의 nickname 등을 클로저로 참조하여 제대로 작동하지 않았음.
*수정 사항
useCallback 추가: handleWebSocketMessage를 useCallback으로 감싸서 안정적인 참조를 만들고, dependency에 nickname을 추가함.
함수 위치 이동: handleWebSocketMessage를 useEffect 앞으로 이동하여 선언 순서 문제를 해결함.
dependency 배열 업데이트: useEffect의 dependency에 handleWebSocketMessage를 추가하여 함수가 변경되면 핸들러를 재등록하도록 함.
불필요한 코드 제거: 사용하지 않는 wsUrl 변수와 중복 함수 정의를 제거함.
🔢 이슈 번호
❗️PR Point
📸 스크린샷