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(core): wrap setFiltersFn with useCallback #6386

Merged
merged 3 commits into from
Oct 14, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/spotty-jokes-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@refinedev/core": patch
---

fix(core): wrap `setFilters` and `setSorters` methods with `useCallback` to prevent looping re-renders

With this we can use the setFilters as dependencies inside useEffects without infinite loop since state changes in the hook won't cause the functions to be re-assigned

[Fixes #6385](https://github.com/refinedev/refine/issues/6385)
78 changes: 47 additions & 31 deletions packages/core/src/hooks/useTable/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from "react";
import React, { useState, useEffect, useCallback } from "react";

import type {
QueryObserverResult,
Expand Down Expand Up @@ -520,40 +520,56 @@ export function useTable<
dataProviderName,
});

const setFiltersAsMerge = (newFilters: CrudFilter[]) => {
setFilters((prevFilters) =>
unionFilters(preferredPermanentFilters, newFilters, prevFilters),
);
};
const setFiltersAsMerge = useCallback(
(newFilters: CrudFilter[]) => {
setFilters((prevFilters) =>
unionFilters(preferredPermanentFilters, newFilters, prevFilters),
);
},
[preferredPermanentFilters],
);

const setFiltersAsReplace = (newFilters: CrudFilter[]) => {
setFilters(unionFilters(preferredPermanentFilters, newFilters));
};
const setFiltersAsReplace = useCallback(
(newFilters: CrudFilter[]) => {
setFilters(unionFilters(preferredPermanentFilters, newFilters));
},
[preferredPermanentFilters],
);

const setFiltersWithSetter = (
setter: (prevFilters: CrudFilter[]) => CrudFilter[],
) => {
setFilters((prev) => unionFilters(preferredPermanentFilters, setter(prev)));
};
const setFiltersWithSetter = useCallback(
(setter: (prevFilters: CrudFilter[]) => CrudFilter[]) => {
setFilters((prev) =>
unionFilters(preferredPermanentFilters, setter(prev)),
);
},
[preferredPermanentFilters],
);

const setFiltersFn: useTableReturnType<TQueryFnData>["setFilters"] = (
setterOrFilters,
behavior: SetFilterBehavior = prefferedFilterBehavior,
) => {
if (typeof setterOrFilters === "function") {
setFiltersWithSetter(setterOrFilters);
} else {
if (behavior === "replace") {
setFiltersAsReplace(setterOrFilters);
} else {
setFiltersAsMerge(setterOrFilters);
}
}
};
const setFiltersFn: useTableReturnType<TQueryFnData>["setFilters"] =
useCallback(
(
setterOrFilters,
behavior: SetFilterBehavior = prefferedFilterBehavior,
) => {
if (typeof setterOrFilters === "function") {
setFiltersWithSetter(setterOrFilters);
} else {
if (behavior === "replace") {
setFiltersAsReplace(setterOrFilters);
} else {
setFiltersAsMerge(setterOrFilters);
}
}
},
[setFiltersWithSetter, setFiltersAsReplace, setFiltersAsMerge],
);

const setSortWithUnion = (newSorter: CrudSort[]) => {
setSorters(() => unionSorters(preferredPermanentSorters, newSorter));
};
const setSortWithUnion = useCallback(
(newSorter: CrudSort[]) => {
setSorters(() => unionSorters(preferredPermanentSorters, newSorter));
},
[preferredPermanentSorters],
);

const { elapsedTime } = useLoadingOvertime({
isLoading: queryResult.isFetching,
Expand Down
Loading