codestates / codestates/triplus

[Error] 상태를 명확하게 보낼 것

Open
#202 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
0
Forks
4
PR merge metrics
No merged PRs in 30d

Description

## 어떤 에러가 발생했나요?
- 타입이 배열인 상태를 map 으로 돌려 채팅 말풍선을 생성하는데, 상태가 하나 추가 될 때 이전 상태에 덧붙여지는 방식으로 갱신되는 것이 아니라 리셋된 후에 추가 된다는 문제가 있었다.

### 에러가 발생한 폴더
component > pages > ChattingPage

### 에러 원인
- socket.io로 실시간 통신하고 있을 때, socket 으로 이벤트를 받은 그 시점에서 상태를 조회하니 이전의 상태와는 상관없이 오직 빈배열만 나왔다.
- 그러니 상태를 갱신할 때 이전 상태에 덧붙여지는 것이 아니라 빈배열에서 상태를 갱신하는 꼴이 된 것

### 에러 해결 방법

```jsx
// 이전 코드
setChatBubble([...chatBubble, upState]);

// 변경한 코드
setChatBubble((chatBubble) => [...chatBubble, upState]);
```

## 에러 해결 관련 링크
- 본인 블로그 링크
https://je-developing.tistory.com/170

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.