Skip to content

Commit

Permalink
chore: optimize code by adding depsEqual function (#2216)
Browse files Browse the repository at this point in the history
  • Loading branch information
maomao1996 committed Jun 19, 2023
1 parent 87b3ade commit 5da4cc4
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
6 changes: 1 addition & 5 deletions packages/hooks/src/createDeepCompareEffect/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { useRef } from 'react';
import type { DependencyList, useEffect, useLayoutEffect } from 'react';
import isEqual from 'lodash/isEqual';
import { depsEqual } from '../utils/depsEqual';

type EffectHookType = typeof useEffect | typeof useLayoutEffect;
type CreateUpdateEffect = (hook: EffectHookType) => EffectHookType;

const depsEqual = (aDeps: DependencyList = [], bDeps: DependencyList = []) => {
return isEqual(aDeps, bDeps);
};

export const createDeepCompareEffect: CreateUpdateEffect = (hook) => (effect, deps) => {
const ref = useRef<DependencyList>();
const signalRef = useRef<number>(0);
Expand Down
5 changes: 5 additions & 0 deletions packages/hooks/src/utils/depsEqual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { DependencyList } from 'react';
import isEqual from 'lodash/isEqual';

export const depsEqual = (aDeps: DependencyList = [], bDeps: DependencyList = []) =>
isEqual(aDeps, bDeps);
6 changes: 1 addition & 5 deletions packages/hooks/src/utils/useDeepCompareWithTarget.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import isEqual from 'lodash/isEqual';
import type { DependencyList, EffectCallback } from 'react';
import { useRef } from 'react';
import type { BasicTarget } from './domTarget';
import useEffectWithTarget from './useEffectWithTarget';

const depsEqual = (aDeps: DependencyList, bDeps: DependencyList = []) => {
return isEqual(aDeps, bDeps);
};
import { depsEqual } from './depsEqual';

const useDeepCompareEffectWithTarget = (
effect: EffectCallback,
Expand Down

0 comments on commit 5da4cc4

Please sign in to comment.