Skip to content

Commit

Permalink
Add useUpdateEffect hook to fix collection search (#944)
Browse files Browse the repository at this point in the history
  • Loading branch information
harshithmohan authored Jul 6, 2024
1 parent 7488d46 commit 4215afc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/hooks/useUpdateEffect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useEffect, useRef } from 'react';
import type { DependencyList, EffectCallback } from 'react';

const useUpdateEffect = (effect: EffectCallback, dependencies: DependencyList) => {
const isInitialMount = useRef(true);

// eslint-disable-next-line consistent-return
useEffect(() => {
if (isInitialMount.current) {
isInitialMount.current = false;
} else {
return effect();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, dependencies);
};

export default useUpdateEffect;
5 changes: 2 additions & 3 deletions src/pages/collection/Collection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { useSettingsQuery } from '@/core/react-query/settings/queries';
import { useGroupViewQuery } from '@/core/react-query/webui/queries';
import useEventCallback from '@/hooks/useEventCallback';
import useFlattenListResult from '@/hooks/useFlattenListResult';
import useUpdateEffect from '@/hooks/useUpdateEffect';

import type { RootState } from '@/core/store';
import type { FilterCondition, FilterType, SortingCriteria } from '@/core/types/api/filter';
Expand Down Expand Up @@ -109,10 +110,8 @@ function Collection() {
setMode(viewSetting);
}, [viewSetting]);

useEffect(() => {
useUpdateEffect(() => {
setSearch('');
// Shouldn't be emptied on every render
// eslint-disable-next-line react-hooks/exhaustive-deps -- for setSearch
}, [isSeries]);

const groupFilterCondition = useMemo(() => {
Expand Down

0 comments on commit 4215afc

Please sign in to comment.