-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed memory leaks in MonkAppStateProvider and LiveConfigAppProvide…
…r components (#882) * Added useIsMounted hook * Removed memory leaks in MonkAppStateProvider and LiveConfigAppProvider components
- Loading branch information
1 parent
1ce5f6e
commit c53e15d
Showing
7 changed files
with
82 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
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,19 @@ | ||
import { useCallback, useEffect, useRef } from 'react'; | ||
|
||
/** | ||
* Custom hook returning a ref to a util function returning `true` if the component using the hook is mounted, and false | ||
* otherwise. | ||
*/ | ||
export function useIsMounted(): () => boolean { | ||
const isMounted = useRef(false); | ||
|
||
useEffect(() => { | ||
isMounted.current = true; | ||
|
||
return () => { | ||
isMounted.current = false; | ||
}; | ||
}, []); | ||
|
||
return useCallback(() => isMounted.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { useIsMounted } from '../../src/hooks/useIsMounted'; | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
|
||
describe('useIsMounted hook', () => { | ||
it('should return true when the component is mounted and false when unmounted', () => { | ||
const { result, unmount } = renderHook(useIsMounted); | ||
expect(result.current()).toBe(true); | ||
unmount(); | ||
expect(result.current()).toBe(false); | ||
}); | ||
}); |