-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
116 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,41 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import _ from 'lodash'; | ||
import { useRef, useEffect } from 'react'; | ||
import type { DependencyList, EffectCallback } from 'react'; | ||
import deepEqual from 'fast-deep-equal/react'; | ||
import { isPrimitive } from '@planjs/utils'; | ||
|
||
const warnDeps = (dependencies: DependencyList) => { | ||
if (dependencies.length === 0) { | ||
console.warn('useDeepEffect should not be used with no dependencies. Use useEffect instead.'); | ||
} | ||
|
||
if (dependencies.every(isPrimitive)) { | ||
console.warn('useDeepEffect should not be used with primitive values. Use useEffect instead.'); | ||
} | ||
}; | ||
|
||
function useDeepCompareMemoize(value: DependencyList) { | ||
const ref = useRef<DependencyList>(); | ||
const signalRef = useRef<number>(0); | ||
|
||
if (!deepEqual(value, ref.current)) { | ||
ref.current = value; | ||
signalRef.current += 1; | ||
} | ||
|
||
return [signalRef.current]; | ||
} | ||
|
||
/** | ||
* 使用上与 useEffect 完全相同,只是它进行了深比较 | ||
* @param effect | ||
* @param fn | ||
* @param deps | ||
*/ | ||
const useDeepEffect: typeof useEffect = (effect, deps) => { | ||
const oldDepsRef = useRef<React.DependencyList>([]); | ||
|
||
useEffect(() => { | ||
const isUpdate = oldDepsRef.current.some((dep, index) => { | ||
return !_.isEqual(dep, deps?.[index]); | ||
}); | ||
|
||
if (isUpdate) { | ||
oldDepsRef.current = deps || []; | ||
return effect(); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, deps); | ||
}; | ||
function useDeepEffect(fn: EffectCallback, deps: DependencyList): ReturnType<typeof useEffect> { | ||
if (process.env.NODE_ENV !== 'production') { | ||
warnDeps(deps); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
return useEffect(fn, useDeepCompareMemoize(deps)); | ||
} | ||
|
||
export default useDeepEffect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.