@@ -33,20 +33,28 @@ export const useInfiniteScroll = <DataItem, Args extends InfinityListArgs>({
3333 const [ totalCount , setTotalCount ] = useState < number | void > ( ) ;
3434 const scrollElement = useRef < HTMLElement > ( document . documentElement ) ;
3535 const isLoadingRef = useRef < boolean > ( false ) ;
36+ const isDisabledMoreRef = useRef < boolean > ( false ) ;
3637 const lastRequestParams = useRef < Args | undefined > ( undefined ) ;
37- const [ disabledMore , setDisabledMore ] = useState ( false ) ;
3838 const { limit, ...argsProp } = args ;
3939 const lastArgsProps = useRef < Partial < Args > > ( null ) ;
4040
4141 const [ getItems , { isLoading, isFetching } ] = useLazyQuery ( { ...args } as Args ) ;
4242
4343 const getDataRequest = ( params : Args ) => {
44- lastRequestParams . current = params ;
44+ if ( isEqual ( params , lastRequestParams . current ) ) {
45+ return Promise . reject ( ) ;
46+ }
4547
46- return getItems ( {
48+ const request = getItems ( {
4749 limit,
4850 ...params ,
4951 } as Args ) . unwrap ( ) ;
52+
53+ request . then ( ( ) => {
54+ lastRequestParams . current = { ...params } ;
55+ } ) ;
56+
57+ return request ;
5058 } ;
5159
5260 const getEmptyList = ( ) => {
@@ -55,7 +63,8 @@ export const useInfiniteScroll = <DataItem, Args extends InfinityListArgs>({
5563 setData ( [ ] ) ;
5664
5765 getDataRequest ( argsProp as Args ) . then ( ( result : LazyQueryResponse < DataItem > ) => {
58- setDisabledMore ( false ) ;
66+ // setDisabledMore(false);
67+ isDisabledMoreRef . current = false ;
5968
6069 if ( 'data' in result ) {
6170 setData ( result . data as ListResponse < DataItem > ) ;
@@ -77,7 +86,7 @@ export const useInfiniteScroll = <DataItem, Args extends InfinityListArgs>({
7786 } , [ argsProp , lastArgsProps , skip ] ) ;
7887
7988 const getMore = async ( ) => {
80- if ( isLoadingRef . current || disabledMore || skip ) {
89+ if ( isLoadingRef . current || isDisabledMoreRef . current || skip ) {
8190 return ;
8291 }
8392
@@ -102,15 +111,15 @@ export const useInfiniteScroll = <DataItem, Args extends InfinityListArgs>({
102111 if ( listResponse . length > 0 ) {
103112 setData ( ( prev ) => [ ...prev , ...listResponse ] ) ;
104113 } else {
105- setDisabledMore ( true ) ;
114+ isDisabledMoreRef . current = true ;
106115 }
107116 } catch ( e ) {
108117 console . log ( e ) ;
109118 }
110119
111120 setTimeout ( ( ) => {
112121 isLoadingRef . current = false ;
113- } , 10 ) ;
122+ } , 50 ) ;
114123 } ;
115124
116125 useLayoutEffect ( ( ) => {
@@ -124,7 +133,7 @@ export const useInfiniteScroll = <DataItem, Args extends InfinityListArgs>({
124133 } , [ data ] ) ;
125134
126135 const onScroll = useCallback ( ( ) => {
127- if ( disabledMore || isLoadingRef . current ) {
136+ if ( isDisabledMoreRef . current || isLoadingRef . current ) {
128137 return ;
129138 }
130139
@@ -135,7 +144,7 @@ export const useInfiniteScroll = <DataItem, Args extends InfinityListArgs>({
135144 if ( scrollPositionFromBottom < SCROLL_POSITION_GAP ) {
136145 getMore ( ) . catch ( console . log ) ;
137146 }
138- } , [ disabledMore , getMore ] ) ;
147+ } , [ getMore ] ) ;
139148
140149 useEffect ( ( ) => {
141150 document . addEventListener ( 'scroll' , onScroll ) ;
0 commit comments