-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
44 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useIsMountedRef } from './useIsMountedRef'; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
expect.hasAssertions(); | ||
}); | ||
|
||
describe('useIsMountedRef', () => { | ||
it('should return a ref which tracks whether the component is mounted or not', () => { | ||
const { result, unmount } = renderHook(() => useIsMountedRef()); | ||
|
||
expect(result.current.current).toBe(true); | ||
|
||
unmount(); | ||
|
||
expect(result.current.current).toBe(false); | ||
}); | ||
}); |
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,20 @@ | ||
import { useEffect, useRef } from 'react'; | ||
|
||
/** | ||
* Returns a ref which tracks whether the component is mounted or not. | ||
*/ | ||
export function useIsMountedRef() { | ||
const isMountedRef = useRef(false); | ||
|
||
useEffect(() => { | ||
isMountedRef.current = true; | ||
|
||
return () => { | ||
isMountedRef.current = false; | ||
}; | ||
}, []); | ||
|
||
return isMountedRef; | ||
} | ||
|
||
export default useIsMountedRef; |