Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix <ListGuesser> flashes when changing sort or pagination #9606

Merged
merged 1 commit into from
Jan 24, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions packages/ra-ui-materialui/src/list/ListGuesser.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,38 @@ export const LinkedShow = () => (
</AdminUI>
</AdminContext>
);

const delayedDataProvider = new Proxy(dataProvider, {
get: (target, name) => (resource, params) => {
if (typeof name === 'symbol' || name === 'then') {
return;
}
return new Promise(resolve =>
setTimeout(() => resolve(dataProvider[name](resource, params)), 300)
);
},
});
export const ManyResources = () => (
<AdminContext
dataProvider={delayedDataProvider}
i18nProvider={polyglotI18nProvider(() => defaultMessages, 'en')}
>
<AdminUI>
<Resource
name="products"
list={ListGuesser}
recordRepresentation="name"
/>
<Resource
name="categories"
list={ListGuesser}
recordRepresentation="name"
/>
<Resource
name="tags"
list={ListGuesser}
recordRepresentation="name"
/>
</AdminUI>
</AdminContext>
);
12 changes: 11 additions & 1 deletion packages/ra-ui-materialui/src/list/ListGuesser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import {
useListContext,
useResourceContext,
RaRecord,
usePrevious,
useResourceDefinition,
} from 'ra-core';
import { useLocation } from 'react-router';

import { ListProps } from './List';
import { ListView, ListViewProps } from './ListView';
Expand Down Expand Up @@ -51,6 +53,14 @@ export const ListGuesser = <RecordType extends RaRecord = any>(
sort,
...rest
} = props;
// force a rerender of this component when any list parameter changes
// otherwise the ListBase won't be rerendered when the sort changes
// and the following check won't be performed
useLocation();
// keep previous data, unless the resource changes
const resourceFromContext = useResourceContext(props);
const previousResource = usePrevious(resourceFromContext);
const keepPreviousData = previousResource === resourceFromContext;
return (
<ListBase<RecordType>
debounce={debounce}
Expand All @@ -60,7 +70,7 @@ export const ListGuesser = <RecordType extends RaRecord = any>(
filter={filter}
filterDefaultValues={filterDefaultValues}
perPage={perPage}
queryOptions={{ keepPreviousData: false }}
queryOptions={{ keepPreviousData }}
resource={resource}
sort={sort}
>
Expand Down