-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NO CHANGELOG][Checkout Widget] Notify iframe if widget was initialis…
…ed with a provider (#2147)
- Loading branch information
Showing
2 changed files
with
45 additions
and
8 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
23 changes: 23 additions & 0 deletions
23
packages/checkout/widgets-lib/src/widgets/checkout/hooks/useMount.ts
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,23 @@ | ||
import { useEffect, useRef } from 'react'; | ||
|
||
/** | ||
* This hook is used to run an effect only once when after the component is mounted. | ||
* and all dependencies are either by default not undefined or the compare function returns true. | ||
*/ | ||
export const useMount = <T, D extends readonly T[]>( | ||
effect: () => void, | ||
deps: D, | ||
compare?: (deps: D) => boolean, | ||
) => { | ||
const hasRunRef = useRef(false); | ||
|
||
useEffect(() => { | ||
const allDepsSatisfy = compare | ||
? compare(deps) | ||
: deps.every((dep) => dep !== undefined); | ||
if (!hasRunRef.current && allDepsSatisfy) { | ||
hasRunRef.current = true; | ||
effect(); | ||
} | ||
}, [deps, effect, compare]); | ||
}; |