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 useListParams might navigate to an old pathname #7882

Merged
merged 2 commits into from
Jun 24, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 69 additions & 58 deletions packages/ra-core/src/controller/list/useListParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,64 +128,69 @@ export const useListParams = ({
}
}, [location.search]); // eslint-disable-line

const changeParams = useCallback(action => {
// do not change params if the component is already unmounted
// this is necessary because changeParams can be debounced, and therefore
// executed after the component is unmounted
if (!isMounted.current) return;

if (!tempParams.current) {
// no other changeParams action dispatched this tick
tempParams.current = queryReducer(query, action);
// schedule side effects for next tick
setTimeout(() => {
if (disableSyncWithLocation) {
setLocalParams(tempParams.current);
} else {
// the useEffect above will apply the changes to the params in the store
navigate(
{
search: `?${stringify({
...tempParams.current,
filter: JSON.stringify(
tempParams.current.filter
),
displayedFilters: JSON.stringify(
tempParams.current.displayedFilters
),
})}`,
},
{
state: { _scrollToTop: action.type === SET_PAGE },
}
);
}
tempParams.current = undefined;
}, 0);
} else {
// side effects already scheduled, just change the params
tempParams.current = queryReducer(tempParams.current, action);
}
}, requestSignature); // eslint-disable-line react-hooks/exhaustive-deps
const changeParams = useCallback(
action => {
// do not change params if the component is already unmounted
// this is necessary because changeParams can be debounced, and therefore
// executed after the component is unmounted
if (!isMounted.current) return;

if (!tempParams.current) {
// no other changeParams action dispatched this tick
tempParams.current = queryReducer(query, action);
// schedule side effects for next tick
setTimeout(() => {
if (disableSyncWithLocation) {
setLocalParams(tempParams.current);
} else {
// the useEffect above will apply the changes to the params in the store
navigate(
{
search: `?${stringify({
...tempParams.current,
filter: JSON.stringify(
tempParams.current.filter
),
displayedFilters: JSON.stringify(
tempParams.current.displayedFilters
),
})}`,
},
{
state: {
_scrollToTop: action.type === SET_PAGE,
},
}
);
}
tempParams.current = undefined;
}, 0);
} else {
// side effects already scheduled, just change the params
tempParams.current = queryReducer(tempParams.current, action);
}
},
[...requestSignature, navigate] // eslint-disable-line react-hooks/exhaustive-deps
);

const setSort = useCallback(
(sort: SortPayload) =>
changeParams({
type: SET_SORT,
payload: sort,
}),
requestSignature // eslint-disable-line react-hooks/exhaustive-deps
[...requestSignature, changeParams] // eslint-disable-line react-hooks/exhaustive-deps
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this means you can remove the eslint disable comment. Same below

);

const setPage = useCallback(
(newPage: number) => changeParams({ type: SET_PAGE, payload: newPage }),
requestSignature // eslint-disable-line react-hooks/exhaustive-deps
[...requestSignature, changeParams] // eslint-disable-line react-hooks/exhaustive-deps
);

const setPerPage = useCallback(
(newPerPage: number) =>
changeParams({ type: SET_PER_PAGE, payload: newPerPage }),
requestSignature // eslint-disable-line react-hooks/exhaustive-deps
[...requestSignature, changeParams] // eslint-disable-line react-hooks/exhaustive-deps
);

const filterValues = query.filter || emptyObject;
Expand All @@ -212,25 +217,31 @@ export const useListParams = ({
displayedFilters,
},
}),
requestSignature // eslint-disable-line react-hooks/exhaustive-deps
[...requestSignature, changeParams] // eslint-disable-line react-hooks/exhaustive-deps
);

const hideFilter = useCallback((filterName: string) => {
changeParams({
type: HIDE_FILTER,
payload: filterName,
});
}, requestSignature); // eslint-disable-line react-hooks/exhaustive-deps
const hideFilter = useCallback(
(filterName: string) => {
changeParams({
type: HIDE_FILTER,
payload: filterName,
});
},
[...requestSignature, changeParams] // eslint-disable-line react-hooks/exhaustive-deps
);

const showFilter = useCallback((filterName: string, defaultValue: any) => {
changeParams({
type: SHOW_FILTER,
payload: {
filterName,
defaultValue,
},
});
}, requestSignature); // eslint-disable-line react-hooks/exhaustive-deps
const showFilter = useCallback(
(filterName: string, defaultValue: any) => {
changeParams({
type: SHOW_FILTER,
payload: {
filterName,
defaultValue,
},
});
},
[...requestSignature, changeParams] // eslint-disable-line react-hooks/exhaustive-deps
);

return [
{
Expand Down