Skip to content

Commit

Permalink
feat(hook): useUpdateEffect, useIsFirstRender hook 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
saseungmin committed Sep 4, 2023
1 parent da851f9 commit 28a2a26
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-steaks-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nf-team/react": minor
---

feat(hook): useUpdateEffect, useIsFirstRender hook 구현
2 changes: 2 additions & 0 deletions packages/react/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export { default as useActionKeyEvent } from './useActionKeyEvent';
export { default as useBoolean } from './useBoolean';
export { default as useDebounce } from './useDebounce';
export { default as useIsFirstRender } from './useIsFirstRender';
export { default as useIsMounted } from './useIsMounted';
export { default as useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';
export { default as useLessThenScrollY } from './useLessThenScrollY';
export { default as useResizeViewportHeight } from './useResizeViewportHeight';
export { default as useThrottleCallback } from './useThrottleCallback';
export { default as useTimeout } from './useTimeout';
export { default as useUpdateEffect } from './useUpdateEffect';
15 changes: 15 additions & 0 deletions packages/react/src/hooks/useIsFirstRender.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useRef } from 'react';

function useIsFirstRender(): boolean {
const isFirst = useRef(true);

if (isFirst.current) {
isFirst.current = false;

return true;
}

return isFirst.current;
}

export default useIsFirstRender;
16 changes: 16 additions & 0 deletions packages/react/src/hooks/useUpdateEffect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { DependencyList, EffectCallback, useEffect } from 'react';

import useIsFirstRender from './useIsFirstRender';

function useUpdateEffect(effect: EffectCallback, deps?: DependencyList) {
const isFirst = useIsFirstRender();

// eslint-disable-next-line consistent-return
useEffect(() => {
if (!isFirst) {
return effect();
}
}, deps);
}

export default useUpdateEffect;

0 comments on commit 28a2a26

Please sign in to comment.