-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
1 parent
06b3f7b
commit f5f3699
Showing
5 changed files
with
2,068 additions
and
1,787 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React, { useRef } from "react"; | ||
import isEqual from "lodash/isEqual"; | ||
|
||
/** | ||
* Deep compare effect hook. | ||
* Ensures the effect runs on the first render and whenever dependencies deeply change. | ||
* | ||
* @param effect The effect callback function. | ||
* @param dependencies The dependencies array to compare deeply. | ||
*/ | ||
export default function useDeepCompareEffect( | ||
effect: React.EffectCallback, | ||
dependencies: any[] | ||
) { | ||
const previousDependenciesRef = useRef<any[]>(); | ||
const isFirstRender = useRef(true); | ||
|
||
const hasChanged = | ||
isFirstRender.current || !isEqual(previousDependenciesRef.current, dependencies); | ||
|
||
React.useEffect(() => { | ||
if (hasChanged) { | ||
isFirstRender.current = false; // Mark that the first render has passed | ||
previousDependenciesRef.current = dependencies; // Update dependencies reference | ||
return effect(); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [hasChanged]); // Depend only on the change detection flag | ||
}; |
Oops, something went wrong.