-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(app): excessive TokenIcon re-rendering
This is due to the whole token being passed as a prop (causing a re-render) not just the required fragment fields. This should hopefully be resolved with urql-graphql/urql#1408
- Loading branch information
Showing
5 changed files
with
84 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useCallback, useMemo, useRef } from 'react'; | ||
import deepEqual from 'fast-deep-equal'; | ||
|
||
export function useDeepMemo<T>(factory: () => T, dependencies: React.DependencyList) { | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
return useMemo(factory, useDeepCompareMemoize(dependencies)); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-types -- useCallback expects Function | ||
export function useDeepCallback<T extends Function>( | ||
callback: T, | ||
dependencies: React.DependencyList, | ||
) { | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
return useCallback(callback, useDeepCompareMemoize(dependencies)); | ||
} | ||
|
||
function useDeepCompareMemoize(dependencies: React.DependencyList) { | ||
const dependenciesRef = useRef<React.DependencyList>(dependencies); | ||
const signalRef = useRef<number>(0); | ||
|
||
if (!deepEqual(dependencies, dependenciesRef.current)) { | ||
dependenciesRef.current = dependencies; | ||
signalRef.current += 1; | ||
} | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
return useMemo(() => dependenciesRef.current, [signalRef.current]); | ||
} |