-
Notifications
You must be signed in to change notification settings - Fork 56
[2팀 이진희] Chapter 1-3. 프레임워크 없이 SPA 만들기 #7
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
Open
bebusl
wants to merge
21
commits into
hanghae-plus:main
Choose a base branch
from
bebusl:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
e527d00
과제시작
bebusl f1459be
feat: shallowEquals 정의
bebusl 2b91676
feat: deepEquals 정의
bebusl 65b3058
feat: useRef 정의
bebusl 383b433
feat: useMemo 정의
bebusl e292b3e
feat: useCallback 정의
bebusl 8cedac8
feat: useShallowState 훅 정의
bebusl 162d9fe
fix: equals함수에서 빠진 조건 추가
bebusl 9c48c3b
chore: 주석 제거
bebusl a5a4a27
refactor: useShallowEqual 타입 수정
bebusl b33045d
feat: useAutoCallback 정의
bebusl 96c6e6c
feat: hoc memo 정의
bebusl a1c8063
feat: deepMemo 정의
bebusl 51d61d7
chore: gh-pages 배포 관련 설정
bebusl fd00740
feat: useStorage 정의
bebusl 94f7e5e
feat: useStore 구현
bebusl 3c6896c
feat: useRouter 정의
bebusl 9d3ed40
fix: useAutoCallback에서 props받게 수정
bebusl b7eef9e
feat: ToastProvider 개선
bebusl 6c53a52
refactor: euqals refactor
bebusl 52320e3
refactor: 변수정도만 정리
bebusl 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 |
|---|---|---|
| @@ -1,7 +1,21 @@ | ||
| import { useState } from "react"; | ||
| import { useRef, useState } from "react"; | ||
| import { useCallback } from "./useCallback"; | ||
| import { shallowEquals } from "../equals"; | ||
|
|
||
| export const useShallowState = <T>(initialValue: Parameters<typeof useState<T>>[0]) => { | ||
| // useState를 사용하여 상태를 관리하고, shallowEquals를 사용하여 상태 변경을 감지하는 훅을 구현합니다. | ||
| return useState(initialValue); | ||
| const [state, _setState] = useState<T | undefined>(initialValue); | ||
| const curState = useRef<T>(state); //setState 메모이제이션용. state를 의존해서는 안됨. -> 매번 리렌더링 됨 | ||
|
|
||
| curState.current = state; | ||
|
|
||
| const setState = useCallback((updater: T | ((prev: T) => T)) => { | ||
| const newValue = | ||
| typeof updater === "function" ? (updater as (prev: T | undefined) => T)(curState.current) : updater; | ||
|
|
||
| if (!shallowEquals(newValue, curState.current)) { | ||
| _setState(newValue); | ||
| } | ||
| }, []); | ||
|
|
||
| return [state, setState]; | ||
| }; | ||
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.
useRef 를 사용해 state를 저장한 이유가 매번 리렌더링 되는걸 막기 위해서인가요!? 매번 리렌더링이 되는 이유가 궁금합니다!
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.
제가 만든 setState 함수에서 state를 직접 참조하면, state가 변경될 때마다 의존성 배열에 state가 들어가게 되고, 그 결과 state가 업데이트될 때마다 setState가 매번 새로 생성됩니다.
그래서 useRef를 사용해 상태를 관리함으로써 setState 함수가 불필요하게 재생성되는 걸 막고 동시에 최신 상태에 안정적으로 접근할 수 있게 했습니다.
제가 '리렌더링'이라고 표현을 좀 잘못 사용한 것 같아요!
리렌더링을 막기 위함이 아니라 setState함수가 매번 재생성되는 걸 막기 위해 ref를 썼다고 보심 될 것 같아요