Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hook): useUpdateEffect, useIsFirstRender hook 구현 #45

Merged
merged 1 commit into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

Check warning on line 13 in packages/react/src/hooks/useUpdateEffect.ts

View workflow job for this annotation

GitHub Actions / check unit test & lint

React Hook useEffect was passed a dependency list that is not an array literal. This means we can't statically verify whether you've passed the correct dependencies

Check warning on line 13 in packages/react/src/hooks/useUpdateEffect.ts

View workflow job for this annotation

GitHub Actions / check unit test & lint

React Hook useEffect has missing dependencies: 'effect' and 'isFirst'. Either include them or remove the dependency array. If 'effect' changes too often, find the parent component that defines it and wrap that definition in useCallback
}

export default useUpdateEffect;
Loading