Skip to content

Commit

Permalink
useURLState unecessary re-rendering bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
manojVivek committed Aug 24, 2023
1 parent 9c8b3a5 commit 6741207
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion ui/packages/shared/components/src/hooks/useURLState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ interface Props {
withURLUpdate?: boolean;
}

const isEqual = (a: string | string[] | undefined, b: string | string[] | undefined): boolean => {
if (typeof a === 'string' && typeof b === 'string') {
return a === b;
}
if (Array.isArray(a) && Array.isArray(b)) {
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false;
}
return true;
}
if (a === undefined && b === undefined) {
return true;
}
return false;
};

export const useURLState = ({
param,
navigateTo,
Expand Down Expand Up @@ -56,7 +72,7 @@ export const useURLState = ({
val === undefined || val == null || val === '';

if (withURLUpdate && navigateTo !== undefined) {
if (router[param] !== value) {
if (!isEqual(router[param], value)) {

This comment has been minimized.

Copy link
@manojVivek

manojVivek Aug 24, 2023

Author Contributor

Array comparisons with === always end up as false, leading to setting the same value again and again.

const searchParams = router;
searchParams[param] = value;

Expand Down

0 comments on commit 6741207

Please sign in to comment.