-
Notifications
You must be signed in to change notification settings - Fork 56
[1팀 김휘린] Chapter 1-3. React, Beyond the Basics #38
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
Hwirin-Kim
wants to merge
18
commits into
hanghae-plus:main
Choose a base branch
from
Hwirin-Kim: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 all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
0b24e45
feat : shallowEquals 완성
Hwirin-Kim b1f389a
feat : shallowEquals 완성
Hwirin-Kim aba016d
feat : deepEquals 구현 완료
Hwirin-Kim a50a516
feat : useRef 완료
Hwirin-Kim 5f6b2e7
feat : useMemo 완성
Hwirin-Kim abf4315
feat : useCallback 구현 완료
Hwirin-Kim 3e69721
feat : useShallowState 완성
Hwirin-Kim c29941a
feat : useAutoCallback 완성
Hwirin-Kim 0549340
feat : HOC memo 완성
Hwirin-Kim 15bb71a
feat : HOC deepMemo 완성
Hwirin-Kim 10ce897
unsubscribe 누락 추가
Hwirin-Kim 1afb8e4
unsubscribe 누락 추가
Hwirin-Kim d3a8f26
feat : useStorage 완성
Hwirin-Kim 5db9ef4
feat : useShallowSelector 완성
Hwirin-Kim de82663
feat : useStore 완성
Hwirin-Kim 5a3a998
feat : useRouter 완성
Hwirin-Kim 04856b0
feat : toast 최적화
Hwirin-Kim 2ebe2be
fix : useRouter 구독해지함수 추가
Hwirin-Kim 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
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
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
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,36 @@ | ||
| import { isPlainObject } from "../utils/typeChecker"; | ||
|
|
||
| export const deepEquals = (a: unknown, b: unknown) => { | ||
| return a === b; | ||
| // 원시 데이터 타입 비교 | ||
| if (Object.is(a, b)) return true; | ||
|
|
||
| // 배열 비교 | ||
| if (Array.isArray(a) && Array.isArray(b)) { | ||
| // 길이가 다르면 false | ||
| if (a.length !== b.length) return false; | ||
| // 요소를 하나씩 비교 | ||
| for (let i = 0; i < a.length; i++) { | ||
| // 요소가 다르면 false | ||
| if (!deepEquals(a[i], b[i])) return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| // 객체 비교 | ||
| if (isPlainObject(a) && isPlainObject(b)) { | ||
| // 객체 키의 개수가 다르면 false | ||
| const aKeys = Object.keys(a); | ||
| const bKeys = Object.keys(b); | ||
| if (aKeys.length !== bKeys.length) return false; | ||
| // 객체 키를 하나씩 비교 | ||
| for (let i = 0; i < aKeys.length; i++) { | ||
| const currentKey = aKeys[i]; | ||
| // 객체 키가 다르면 false | ||
| if (!deepEquals(a[currentKey], b[currentKey])) return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| // 객체 비교 | ||
| return false; | ||
| }; |
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,36 @@ | ||
| import { isPlainObject } from "../utils/typeChecker"; | ||
|
|
||
| export const shallowEquals = (a: unknown, b: unknown) => { | ||
| return a === b; | ||
| // 원시 데이터 타입 비교 | ||
| if (Object.is(a, b)) return true; | ||
|
|
||
| // 배열 비교 | ||
| if (Array.isArray(a) && Array.isArray(b)) { | ||
| // 길이가 다르면 false | ||
| if (a.length !== b.length) return false; | ||
| // 요소를 하나씩 비교 | ||
| for (let i = 0; i < a.length; i++) { | ||
| // 요소가 다르면 false | ||
| if (!Object.is(a[i], b[i])) return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| // 객체 비교 | ||
| if (isPlainObject(a) && isPlainObject(b)) { | ||
| // 객체 키의 개수가 다르면 false | ||
| const aKeys = Object.keys(a); | ||
| const bKeys = Object.keys(b); | ||
| if (aKeys.length !== bKeys.length) return false; | ||
| // 객체 키를 하나씩 비교 | ||
| for (let i = 0; i < aKeys.length; i++) { | ||
| const currentKey = aKeys[i]; | ||
| // 객체 키가 다르면 false | ||
| if (!Object.is(a[currentKey], b[currentKey])) return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| // 그외 모두 false | ||
| return false; | ||
| }; | ||
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,5 +1,20 @@ | ||
| import type { FunctionComponent } from "react"; | ||
| import { deepEquals } from "../equals"; | ||
| import { useRef } from "../hooks"; | ||
|
|
||
| export function deepMemo<P extends object>(Component: FunctionComponent<P>) { | ||
| return Component; | ||
| // 훅 사용을 위해 컴포넌트 생성 | ||
| const MemoizedComponent = (props: P) => { | ||
| // 이전 프롭스와 리턴할 결과를 ref로 관리 | ||
| const prevRef = useRef<{ props: P; result: React.ReactNode | null } | null>(null); | ||
|
|
||
| // 이전 프롭스와 현재 프롭스 비교 | ||
| if (!prevRef.current || !deepEquals(prevRef.current.props, props)) { | ||
| // 프롭스가 다르면 새로 렌더링 | ||
| prevRef.current = { props, result: Component(props) as React.ReactNode }; | ||
| } | ||
| // 프롭스가 같으면 이전 결과 재사용 | ||
| return prevRef.current.result; | ||
| }; | ||
| return MemoizedComponent; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 발제 자료를 보면 앞에서 만든 memo를 사용을 하라고 되어있어서 기존에 만들어두신 memo에 2번째 인자를 deepEquals 함수를 넘겨주면 더 간단하게 deepMemo를 완성할 수 있을것 같네용 :) |
||
| } | ||
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,6 +1,20 @@ | ||
| import { type FunctionComponent } from "react"; | ||
| import { shallowEquals } from "../equals"; | ||
| import { useRef } from "../hooks"; | ||
|
|
||
| export function memo<P extends object>(Component: FunctionComponent<P>, equals = shallowEquals) { | ||
| return Component; | ||
| // 훅 사용을 위해 컴포넌트 생성 | ||
| const MemoizedComponent = (props: P) => { | ||
| // 이전 프롭스와 리턴할 결과를 ref로 관리 | ||
| const prevRef = useRef<{ props: P; result: React.ReactNode | null } | null>(null); | ||
|
|
||
| // 이전 프롭스와 현재 프롭스 비교 | ||
| if (!prevRef.current || !equals(prevRef.current.props, props)) { | ||
| // 프롭스가 다르면 새로 렌더링 | ||
| prevRef.current = { props, result: Component(props) as React.ReactNode }; | ||
| } | ||
| // 프롭스가 같으면 이전 결과 재사용 | ||
| return prevRef.current.result; | ||
| }; | ||
| return MemoizedComponent; | ||
| } |
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,7 +1,13 @@ | ||
| /* eslint-disable @typescript-eslint/no-unused-vars,@typescript-eslint/no-unsafe-function-type */ | ||
| /* eslint-disable @typescript-eslint/no-unsafe-function-type */ | ||
| import type { DependencyList } from "react"; | ||
| import { useMemo } from "./useMemo"; | ||
|
|
||
| export function useCallback<T extends Function>(factory: T, _deps: DependencyList) { | ||
| // 직접 작성한 useMemo를 통해서 만들어보세요. | ||
| return factory as T; | ||
|
|
||
| // 함수 자체를 값이라고 생각하고 useMemo를 사용해서 구현 | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| const memoizedCallback = useMemo(() => factory, _deps); | ||
|
|
||
| return memoizedCallback as T; | ||
| } |
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,8 +1,18 @@ | ||
| /* eslint-disable @typescript-eslint/no-unused-vars */ | ||
| import type { DependencyList } from "react"; | ||
| import { shallowEquals } from "../equals"; | ||
| import { useRef } from "./useRef"; | ||
|
|
||
| export function useMemo<T>(factory: () => T, _deps: DependencyList, _equals = shallowEquals): T { | ||
| // 직접 작성한 useRef를 통해서 만들어보세요. | ||
| return factory(); | ||
|
|
||
| // 의존성 배열과 값을 저장하는 객체를 useRef로 관리 | ||
| const ref = useRef<{ deps: DependencyList; value: T } | null>(null); | ||
|
|
||
| // 의존성 배열이 바뀌었거나, 값이 바뀌었으면 factory 실행해서 값 저장 | ||
| if (ref.current === null || !_equals(ref.current.deps, _deps)) { | ||
| const value = factory(); | ||
| ref.current = { deps: _deps, value }; | ||
| } | ||
|
|
||
| return ref.current.value; | ||
| } |
Oops, something went wrong.
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.
배열의 타입은 'object'여서 이 코드가 없어도 테스트가 통과할 거에요 지금 utils 함수에서 배열 타입 체크 부분만 빼면요!
Object.keys() 메서드를 사용시 인덱스가 key가 됩니다 :)