File tree Expand file tree Collapse file tree 1 file changed +20
-6
lines changed
src/components/ChannelList/hooks Expand file tree Collapse file tree 1 file changed +20
-6
lines changed Original file line number Diff line number Diff line change 1- import { useEffect , useRef , useState } from 'react' ;
1+ import { useEffect , useMemo , useRef , useState } from 'react' ;
22
33import { MAX_QUERY_CHANNELS_LIMIT } from '../utils' ;
44
@@ -67,7 +67,7 @@ export const usePaginatedChannels = <
6767 queryType = '' ,
6868 retryCount = 0 ,
6969 ) : Promise < void > => {
70- if ( loadingChannels || loadingNextPage || refreshing ) return ;
70+ if ( ! client || loadingChannels || loadingNextPage || refreshing ) return ;
7171
7272 if ( queryType === 'reload' ) {
7373 setLoadingChannels ( true ) ;
@@ -134,11 +134,25 @@ export const usePaginatedChannels = <
134134
135135 const reloadList = ( ) => queryChannels ( 'reload' ) ;
136136
137+ /**
138+ * Equality check using stringified filters ensure that we don't make un-necessary queryChannels api calls
139+ * for the scenario:
140+ *
141+ * <ChannelList
142+ * filters={{
143+ * members: { $in: ['vishal'] }
144+ * }}
145+ * ...
146+ * />
147+ *
148+ * Here we have passed filters as inline object, which means on every re-render of
149+ * parent component, ChannelList will receive new object reference (even though value is same), which
150+ * in return will trigger useEffect. To avoid this, we can add a value check.
151+ */
152+ const filterStr = useMemo ( ( ) => JSON . stringify ( filters ) , [ filters ] ) ;
137153 useEffect ( ( ) => {
138- if ( client ) {
139- reloadList ( ) ;
140- }
141- } , [ filters ] ) ;
154+ reloadList ( ) ;
155+ } , [ filterStr ] ) ;
142156
143157 return {
144158 channels,
You can’t perform that action at this time.
0 commit comments