-
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.
Merge pull request #51 from mbti-nf-team/feat/unmount-hook
- Loading branch information
Showing
4 changed files
with
43 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@nf-team/react": minor | ||
--- | ||
|
||
feat(useUnmount): useUnmount hook 구현 및 적용 |
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,23 @@ | ||
import { act, renderHook } from '@testing-library/react'; | ||
|
||
import useUnmount from './useUnmount'; | ||
|
||
describe('useUnmount', () => { | ||
const effectCallback = jest.fn(); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
const useUnmountHook = () => renderHook(() => useUnmount(effectCallback)); | ||
|
||
it('unmount되면 콜백함수가 호출되어야만 한다', () => { | ||
const { unmount } = useUnmountHook(); | ||
|
||
act(() => { | ||
unmount(); | ||
}); | ||
|
||
expect(effectCallback).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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,14 @@ | ||
import { useRef } from 'react'; | ||
|
||
import useEffectOnce from './useEffectOnce'; | ||
|
||
function useUnmount(fn: () => void) { | ||
const fnRef = useRef(fn); | ||
|
||
// NOTE - ref 각 렌더를 업데이트하여 변경하면 최신 콜백이 실행 | ||
fnRef.current = fn; | ||
|
||
useEffectOnce(() => () => fnRef.current()); | ||
} | ||
|
||
export default useUnmount; |