-
Notifications
You must be signed in to change notification settings - Fork 1
[FE-Feat] useEffectOnce 훅 구현하기 #428
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ node_modules | |
| dist | ||
| dist-ssr | ||
| *.local | ||
| .env* | ||
|
|
||
| # Editor directories and files | ||
| .vscode/* | ||
|
|
||
7 changes: 5 additions & 2 deletions
7
frontend/apps/client/src/routes/oauth.redirect/calendar/index.tsx
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export * from './useClickOutside'; | ||
| export * from './useEffectOnce'; | ||
| export * from './useSafeContext'; | ||
| export * from './useSelectTime'; |
This file contains hidden or 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,28 @@ | ||
| import { useEffect, useRef } from 'react'; | ||
|
|
||
| interface UseEffectOnceOptions { | ||
| condition?: boolean; | ||
| callback: () => void; | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| * @param options | ||
| * @param options.condition - 콜백을 실행할 조건 (기본값: true) | ||
| * @param options.callback - 한 번만 실행할 콜백 함수. useCallback을 사용하여 메모이제이션하는 것이 좋습니다. | ||
| */ | ||
| export const useEffectOnce = ({ condition = true, callback }: UseEffectOnceOptions) => { | ||
| const isCalled = useRef(false); | ||
|
|
||
| useEffect(() => { | ||
| if (condition && !isCalled.current) { | ||
| callback(); | ||
| } | ||
|
|
||
| return () => { | ||
| if (!isCalled.current) { | ||
| isCalled.current = true; | ||
| } | ||
| }; | ||
| }, [condition, callback]); | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix critical logic error in the hook implementation.
The current implementation has a fundamental flaw in the cleanup function logic that breaks the hook's intended behavior.
Issues:
isCalled.currentis not set totrueimmediately after calling the callbackExample of the bug:
If
conditionis initiallyfalse, the effect runs but doesn't call the callback. The cleanup then setsisCalled.current = true, preventing future execution even whenconditionbecomestrue.Apply this fix:
useEffect(() => { if (condition && !isCalled.current) { + isCalled.current = true; callback(); } - - return () => { - if (!isCalled.current) { - isCalled.current = true; - } - }; }, [condition, callback]);This ensures the callback runs only once when the condition is met, which aligns with the hook's purpose and StrictMode compatibility goals.
📝 Committable suggestion
🤖 Prompt for AI Agents