forked from joshwcomeau/use-sound
-
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.
➕ Add skip feature to workaround joshwcomeau#92
- Loading branch information
1 parent
2265105
commit 9694c80
Showing
8 changed files
with
66 additions
and
32 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,8 @@ | ||
import { DependencyList } from 'react'; | ||
declare const useMount: ( | ||
callback?: Function, | ||
deps?: DependencyList | ||
) => { | ||
isMounted: boolean; | ||
}; | ||
export default useMount; |
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,24 @@ | ||
import { DependencyList, useRef } from 'react'; | ||
import useOnMount from './use-on-mount'; | ||
|
||
const useMount = (callback: Function = () => {}, deps: DependencyList = []) => { | ||
// Ref to track component mount state | ||
const isMounted = useRef(false); | ||
|
||
useOnMount(() => { | ||
// Component has mounted, set the flag | ||
if (!isMounted.current) { | ||
isMounted.current = true; | ||
callback(); | ||
} | ||
|
||
// Cleanup function to reset the flag when component unmounts | ||
return () => { | ||
isMounted.current = false; | ||
}; | ||
}, deps); | ||
|
||
return { isMounted: isMounted.current }; | ||
}; | ||
|
||
export default useMount; |
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,2 +1,6 @@ | ||
import * as React from 'react'; | ||
export default function useOnMount(callback: React.EffectCallback): void; | ||
import { EffectCallback, DependencyList } from 'react'; | ||
declare const useOnMount: ( | ||
callback: EffectCallback, | ||
deps?: DependencyList | ||
) => void; | ||
export default useOnMount; |
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,5 +1,7 @@ | ||
import * as React from 'react'; | ||
import { useEffect, EffectCallback, DependencyList } from 'react'; | ||
|
||
export default function useOnMount(callback: React.EffectCallback) { | ||
React.useEffect(callback, []); | ||
} | ||
const useOnMount = (callback: EffectCallback, deps: DependencyList = []) => { | ||
useEffect(callback, deps); | ||
}; | ||
|
||
export default useOnMount; |