Commit 8a4263a
Use useEffect instead of useDeepCompareEffect in GroupChannelListProvider (#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]>1 parent 8d1bfaa commit 8a4263a
File tree
3 files changed
+19
-8
lines changed- src
- hooks
- modules/GroupChannelList/context
- __tests__
3 files changed
+19
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
17 | | - | |
| 16 | + | |
18 | 17 | | |
19 | | - | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
20 | 22 | | |
21 | | - | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
22 | 34 | | |
23 | 35 | | |
24 | 36 | | |
| |||
Lines changed: 2 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
203 | 203 | | |
204 | 204 | | |
205 | 205 | | |
206 | | - | |
207 | 206 | | |
208 | 207 | | |
209 | | - | |
210 | 208 | | |
211 | 209 | | |
| 210 | + | |
212 | 211 | | |
213 | 212 | | |
214 | 213 | | |
215 | 214 | | |
216 | | - | |
| 215 | + | |
217 | 216 | | |
218 | 217 | | |
219 | 218 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
34 | | - | |
| 34 | + | |
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| |||
0 commit comments