-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
👻 Who Ya Gonna Call?: Fix referential instability in
useSwap
to sto…
…p continuous rerenders and high CPU usage (#3956) * hook and their props changes debug logs swap changes * fix: stabilize switchAssets * fix: stabilize outputDifference * feat: add useDeepMemo to use deep equal when memoizing lodash.isEqual was another option for deep equality checks but fast-deep-equal is lot smaller and more performant * fix: stabilize quote * fix: stabalize useSwapAmountInput this one was the trigger for multiple changes inAmountInput, outAmountInput, spotPriceQuote, sendTradeTokeninTx * fix: stabilize totalFee * Revert "hook and their props changes debug logs" This reverts commit 4b42274. * fix: coderabbit
- Loading branch information
Showing
5 changed files
with
231 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import equal from "fast-deep-equal"; | ||
import { DependencyList, useRef } from "react"; | ||
|
||
/** | ||
* A custom hook for `useMemo` that uses deep comparison on the dependencies. | ||
* | ||
* @param factory - A function that produces the memoized value. | ||
* @param dependencies - The dependency array to be deeply compared. | ||
* @returns The memoized value. | ||
*/ | ||
export function useDeepMemo<T>( | ||
factory: () => T, | ||
dependencies: DependencyList | ||
): T { | ||
if (!Array.isArray(dependencies)) { | ||
throw new Error("useDeepMemo expects a dependency array"); | ||
} | ||
const dependenciesRef = useRef<DependencyList>(); | ||
const memoizedValueRef = useRef<T>(); | ||
|
||
if (!equal(dependenciesRef.current, dependencies)) { | ||
dependenciesRef.current = dependencies; | ||
memoizedValueRef.current = factory(); | ||
} | ||
|
||
return memoizedValueRef.current!; | ||
} |
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