@@ -3,7 +3,7 @@ import get from 'lodash/get';
33import isEqual from 'lodash/isEqual' ;
44import { removeEmpty } from '../../util' ;
55import { FilterPayload , RaRecord , SortPayload } from '../../types' ;
6- import { ResourceContextValue , useResourceContext } from '../../core' ;
6+ import { useResourceContext } from '../../core' ;
77import usePaginationState from '../usePaginationState' ;
88import useSortState from '../useSortState' ;
99import { useRecordSelection } from './useRecordSelection' ;
@@ -91,12 +91,45 @@ export const useList = <RecordType extends RaRecord = any>(
9191 total : data ? data . length : undefined ,
9292 } ) ) ;
9393
94+ // Store pagination states for each storeKey
95+ const storeKeyPaginationRef = useRef < {
96+ [ key : string ] : { page : number ; perPage : number } ;
97+ } > ( { } ) ;
98+
9499 // pagination logic
95100 const { page, setPage, perPage, setPerPage } = usePaginationState ( {
96101 page : initialPage ,
97102 perPage : initialPerPage ,
98103 } ) ;
99104
105+ useEffect ( ( ) => {
106+ if ( ! resource ) return ;
107+ // Check if storeKey exists in the pagination store
108+ const currentPagination = storeKeyPaginationRef . current [ resource ] ;
109+ if ( currentPagination ) {
110+ // Restore existing pagination state for the storeKey
111+ if (
112+ page !== currentPagination . page ||
113+ perPage !== currentPagination . perPage
114+ ) {
115+ setPage ( currentPagination . page ) ;
116+ setPerPage ( currentPagination . perPage ) ;
117+ }
118+ } else {
119+ setPage ( initialPage ) ;
120+ setPerPage ( initialPerPage ) ;
121+ }
122+ storeKeyPaginationRef . current [ resource ] = { page, perPage } ;
123+ } , [
124+ resource ,
125+ setPage ,
126+ setPerPage ,
127+ initialPage ,
128+ initialPerPage ,
129+ page ,
130+ perPage ,
131+ ] ) ;
132+
100133 // sort logic
101134 const { sort, setSort : setSortState } = useSortState ( initialSort ) ;
102135 const setSort = useCallback (
0 commit comments