Skip to content

Commit 4e70e17

Browse files
committed
Don't update location if it has parameters
1 parent 4dd34fc commit 4e70e17

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

packages/ra-core/src/controller/list/useListParams.spec.tsx

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,84 @@ describe('useListParams', () => {
606606
expect(storeValue).toBeUndefined();
607607
});
608608

609+
it('should not synchronize location with store if the location already contains parameters', async () => {
610+
let location;
611+
let storeValue;
612+
const StoreReader = () => {
613+
const [value] = useStore('posts.listParams');
614+
React.useEffect(() => {
615+
storeValue = value;
616+
}, [value]);
617+
return null;
618+
};
619+
render(
620+
<TestMemoryRouter
621+
initialEntries={[
622+
{
623+
search:
624+
'?' +
625+
stringify({
626+
filter: JSON.stringify({}),
627+
sort: 'id',
628+
order: 'ASC',
629+
page: 5,
630+
perPage: 10,
631+
}),
632+
},
633+
]}
634+
locationCallback={l => {
635+
location = l;
636+
}}
637+
>
638+
<CoreAdminContext
639+
dataProvider={testDataProvider()}
640+
store={memoryStore({
641+
'posts.listParams': {
642+
sort: 'id',
643+
order: 'ASC',
644+
page: 10,
645+
perPage: 10,
646+
filter: {},
647+
},
648+
})}
649+
>
650+
<Component disableSyncWithLocation />
651+
<StoreReader />
652+
</CoreAdminContext>
653+
</TestMemoryRouter>
654+
);
655+
656+
await waitFor(() => {
657+
expect(storeValue).toEqual({
658+
sort: 'id',
659+
order: 'ASC',
660+
page: 10,
661+
perPage: 10,
662+
filter: {},
663+
});
664+
});
665+
666+
await waitFor(() => {
667+
expect(location).toEqual(
668+
expect.objectContaining({
669+
hash: '',
670+
key: expect.any(String),
671+
state: null,
672+
pathname: '/',
673+
search:
674+
'?' +
675+
stringify({
676+
filter: JSON.stringify({}),
677+
sort: 'id',
678+
order: 'ASC',
679+
page: 5,
680+
perPage: 10,
681+
}),
682+
})
683+
);
684+
});
685+
});
686+
609687
it('should not synchronize location with store when sync is not enabled', async () => {
610688
let location;
611689
let storeValue;

packages/ra-core/src/controller/list/useListParams.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ export const useListParams = ({
142142
if (disableSyncWithLocation) {
143143
return;
144144
}
145-
if (!isEqual(query, queryFromLocation)) {
145+
if (
146+
!isEqual(query, queryFromLocation) &&
147+
Object.keys(queryFromLocation).length === 0
148+
) {
146149
navigate({
147150
search: `?${stringify({
148151
...query,

0 commit comments

Comments
 (0)