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

[v3] Fix race condition due to debounced setFilter #7444

Merged
merged 2 commits into from
Mar 30, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cypress/integration/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,19 @@ describe('List Page', () => {
LoginPage.login('admin', 'password');
ListPagePosts.navigate();
ListPagePosts.showFilter('title');
// Let's clear the filter first, otherwise we have no way of knowing when the new filter has been (debounced and) applied
ListPagePosts.setFilterValue('title', '');
cy.contains('1-10 of 13');
// Now let's change the filter to something different than the default value
ListPagePosts.setFilterValue(
'title',
'Omnis voluptate enim similique est possimus'
);
cy.contains('1-1 of 1');
// Navigate away and then back
cy.get('[href="#/users"]').click();
cy.get('[href="#/posts"]').click();
// Check that our filter has been preserved
cy.get(ListPagePosts.elements.filter('title')).should(el =>
expect(el).to.have.value(
'Omnis voluptate enim similique est possimus'
Expand Down
7 changes: 7 additions & 0 deletions packages/ra-core/src/controller/useListParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import queryReducer, {
import { changeListParams, ListParams } from '../actions/listActions';
import { SortPayload, ReduxState, FilterPayload } from '../types';
import removeEmpty from '../util/removeEmpty';
import { useIsMounted } from '../util/hooks';

interface ListParamsOptions {
resource: string;
Expand Down Expand Up @@ -126,6 +127,7 @@ const useListParams = ({
shallowEqual
);
const tempParams = useRef<ListParams>();
const isMounted = useIsMounted();

const requestSignature = [
location.search,
Expand Down Expand Up @@ -164,6 +166,11 @@ 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);
Expand Down
10 changes: 10 additions & 0 deletions packages/ra-core/src/util/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,13 @@ export function useTimeout(ms = 0) {

return ready;
}

export function useIsMounted() {
const isMounted = useRef(true);
useEffect(() => {
return () => {
isMounted.current = false;
};
}, []);
return isMounted;
}