Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/hooks/useDeepCompareEffect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,24 @@ function useDeepCompareMemoize<T>(value: T): T {

/**
* Custom hook that works like useEffect but performs a deep comparison of dependencies
* instead of reference equality. This is useful when dealing with complex objects or arrays
* in dependencies that could trigger unnecessary re-renders.
* instead of reference equality.
*
* Inspired by https://github.com/kentcdodds/use-deep-compare-effect
* Best used when:
* - Working with complex objects without guaranteed immutability
* - Handling data from external sources where reference equality isn't maintained
* - Dealing with deeply nested objects where individual memoization is impractical
*
* @param callback Effect callback that can either return nothing (void) or return a cleanup function (() => void).
* Avoid using when:
* - Detecting changes within array items is crucial
* - Performance is critical (deep comparison is expensive)
* - Working primarily with primitive values or simple objects
*
* @example
* useDeepCompareEffect(() => {
* // Effect logic
* }, [complexObject, anotherObject]);
*
* @param callback Effect callback that can return a cleanup function
* @param dependencies Array of dependencies to be deeply compared
*/
function useDeepCompareEffect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,16 @@ export const GroupChannelListManager: React.FC<GroupChannelListProviderProps> =
refresh,
loadMore,
]);
const memoizedGroupChannel = useMemo(() => groupChannels, [groupChannels]);
useDeepCompareEffect(() => {
updateState({
groupChannels: memoizedGroupChannel,
...eventHandlers,
...configurations,
groupChannels,
});
}, [
configurations,
eventHandlers,
memoizedGroupChannel,
groupChannels.map(groupChannel => groupChannel.serialize()),
]);

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jest.mock('@sendbird/uikit-tools', () => ({
useGroupChannelList: jest.fn(() => ({
refreshing: false,
initialized: true,
groupChannels: [{ url: 'test-groupchannel-url-1' }],
groupChannels: [{ url: 'test-groupchannel-url-1', serialize: () => JSON.stringify(this) }],
refresh: null,
loadMore: null,
})),
Expand Down