Skip to content

Conversation

@AhyoungRyu
Copy link
Contributor

@AhyoungRyu AhyoungRyu commented Dec 4, 2024

Fixes

When to use useDeepCompareEffect vs useEffect

useDeepCompareEffect is useful when:

  1. Handling objects without guaranteed immutability
const complexObject = {
  settings: {
    theme: { ... },
    preferences: { ... }
  },
  data: [ ... ]
};

useDeepCompareEffect(() => {
  // When you want to detect actual value changes
}, [complexObject]);
  1. Working with data from external libraries or APIs
  • When objects have new references but identical content
  1. Dealing with deeply nested objects where memoization is impractical
  • When object structures are too complex for individual memoization

useEffect is better when:

  1. Detecting changes in array items
const items = [{id: 1, value: 'a'}, {id: 2, value: 'b'}];
// Better for detecting changes within array items
useEffect(() => {
  // Detect changes in items array
}, [items]);
  1. Performance is critical
  • Deep comparison is computationally expensive
  • Especially important for large arrays or frequently updating data

Example of proper useDeepCompareEffect usage:

useDeepCompareEffect(() => {
  updateState({
    ...configurations,
    ...scrollState,
    ...eventHandlers,
  });
}, [
  configurations,
  scrollState,
  eventHandlers,
]);

This works well here because:

  • Dependencies are mostly objects
  • Updates are needed only when internal structure changes
  • Objects are already memoized, reducing deep comparison cost

Key Takeaway:

  • Use useDeepCompareEffect when structural equality matters
  • Use useEffect for reference equality or primitive value changes
  • Consider the trade-off between performance and accuracy

@netlify
Copy link

netlify bot commented Dec 4, 2024

Deploy Preview for sendbird-uikit-react ready!

Name Link
🔨 Latest commit 725afa2
🔍 Latest deploy log https://app.netlify.com/sites/sendbird-uikit-react/deploys/675194b9067dee00085e9fd3
😎 Deploy Preview https://deploy-preview-1272--sendbird-uikit-react.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@git-babel
Copy link
Contributor

위의 변경점이 어떻게 이슈들을 해결하는지 제가 이해를 못 한 것 같습니다. 혹시 간단한 설명 부탁드릴 수 있을까요?
또, useDeepCompareEffect()를 introduce했던 이유 중 하나는 groupChannels의 경우 useEffect()의 dependency list에 넣을 경우 state update 시 infinite loop가 발생했기 때문이었습니다. 현재 CI에서 발생하고 있는 에러가 해당 에러입니다. 혹시 테스트 케이스쪽에 문제가 있는 거면 수정하겠습니다.

@git-babel git-babel merged commit 8a4263a into feat/state-mgmt-migration-1 Dec 6, 2024
10 checks passed
@AhyoungRyu AhyoungRyu deleted the fix/groupchannel-list-issues branch December 6, 2024 00:30
@AhyoungRyu AhyoungRyu changed the title Use useEffect instead of useDeepCompareEffect in GroupChannelListProvider Fix the wrong group channel dependency Dec 6, 2024
AhyoungRyu added a commit that referenced this pull request Dec 6, 2024
Fixes https://sendbird.atlassian.net/browse/CLNP-5981 and applied the
same approach in
#1272

### Checklist

Put an `x` in the boxes that apply. You can also fill these out after
creating the PR. If unsure, ask the members.
This is a reminder of what we look for before merging your code.

- [x] **All tests pass locally with my changes**
- [ ] **I have added tests that prove my fix is effective or that my
feature works**
- [ ] **Public components / utils / props are appropriately exported**
- [ ] I have added necessary documentation (if appropriate)
AhyoungRyu added a commit that referenced this pull request Dec 11, 2024
…ider (#1272)

Fixes 
- https://sendbird.atlassian.net/browse/CLNP-5966
- https://sendbird.atlassian.net/browse/CLNP-5967
- https://sendbird.atlassian.net/browse/CLNP-5969
- https://sendbird.atlassian.net/browse/CLNP-5971
- https://sendbird.atlassian.net/browse/CLNP-5973

## When to use useDeepCompareEffect vs useEffect

### useDeepCompareEffect is useful when:

1. **Handling objects without guaranteed immutability**
```typescript
const complexObject = {
  settings: {
    theme: { ... },
    preferences: { ... }
  },
  data: [ ... ]
};

useDeepCompareEffect(() => {
  // When you want to detect actual value changes
}, [complexObject]);
```


2. **Working with data from external libraries or APIs**
- When objects have new references but identical content

3. **Dealing with deeply nested objects where memoization is
impractical**
- When object structures are too complex for individual memoization

### useEffect is better when:

1. **Detecting changes in array items**

```typescript
const items = [{id: 1, value: 'a'}, {id: 2, value: 'b'}];
// Better for detecting changes within array items
useEffect(() => {
  // Detect changes in items array
}, [items]);
```


2. **Performance is critical**
- Deep comparison is computationally expensive
- Especially important for large arrays or frequently updating data

### Example of proper useDeepCompareEffect usage:
```typescript
useDeepCompareEffect(() => {
  updateState({
    ...configurations,
    ...scrollState,
    ...eventHandlers,
  });
}, [
  configurations,
  scrollState,
  eventHandlers,
]);
```

This works well here because:
- Dependencies are mostly objects
- Updates are needed only when internal structure changes
- Objects are already memoized, reducing deep comparison cost

### Key Takeaway:
- Use useDeepCompareEffect when structural equality matters
- Use useEffect for reference equality or primitive value changes
- Consider the trade-off between performance and accuracy

---------

Co-authored-by: junyoung.lim <[email protected]>
AhyoungRyu added a commit that referenced this pull request Dec 11, 2024
Fixes https://sendbird.atlassian.net/browse/CLNP-5981 and applied the
same approach in
#1272

### Checklist

Put an `x` in the boxes that apply. You can also fill these out after
creating the PR. If unsure, ask the members.
This is a reminder of what we look for before merging your code.

- [x] **All tests pass locally with my changes**
- [ ] **I have added tests that prove my fix is effective or that my
feature works**
- [ ] **Public components / utils / props are appropriately exported**
- [ ] I have added necessary documentation (if appropriate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants