-
Notifications
You must be signed in to change notification settings - Fork 56
[2팀 학메 진채영]Chapter 1-3. React, Beyond the Basics #53
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
devchaeyoung
wants to merge
16
commits into
hanghae-plus:main
Choose a base branch
from
devchaeyoung: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
16 commits
Select commit
Hold shift + click to select a range
1792c38
feat: shallowEquals 추가
devchaeyoung 622b12a
feat: baseEquals, deepEquals 추가 ->
devchaeyoung 3e06d64
feat: typeGuards 추가
devchaeyoung 340109d
feat: useRef 추가
devchaeyoung ce3e8e2
feat: useMemo 추가
devchaeyoung 9dab656
feat: useCallback 추가
devchaeyoung 70482ee
feat: useShallowState 추가
devchaeyoung b1185b2
feat: useAutoCallback 추가
devchaeyoung dc3d525
feat: deepMemo, memo.ts 추가
devchaeyoung fec0544
style: 주석제거
devchaeyoung 267d166
feat: useStorage 추가
devchaeyoung cc417fb
feat: useStore 추가
devchaeyoung af0605d
fix : createStore 상태 비교 shallowEquals로 변경 후 불필요한 리렌더링 방지
devchaeyoung 166f260
fix: useCallback lint 타입 에러 수정
devchaeyoung 8089826
fix: ToastProvider 커스텀 훅으로 수정
devchaeyoung 7e81b01
fix : useMemo 커스텀훅으로 변경
devchaeyoung 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { isNullish, isPrimitive, isArray, isObject, isSameType } from "../utils"; | ||
|
|
||
| type ComparisonCallback = (a: unknown, b: unknown) => boolean; | ||
|
|
||
| export const baseEquals = (a: unknown, b: unknown, compareValues: ComparisonCallback): boolean => { | ||
| if (a === b) return true; | ||
| if (isNullish(a) || isNullish(b)) return false; | ||
| if (isPrimitive(a) || isPrimitive(b)) return false; | ||
| if (!isSameType(a, b)) return false; | ||
|
|
||
| if (isArray(a) && isArray(b)) { | ||
| if (a.length !== b.length) return false; | ||
| for (let i = 0; i < a.length; i++) { | ||
| if (!compareValues(a[i], b[i])) return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| if (isObject(a) && isObject(b)) { | ||
| const keysA = Object.keys(a); | ||
| const keysB = Object.keys(b); | ||
|
|
||
| if (keysA.length !== keysB.length) return false; | ||
|
|
||
| for (const key of keysA) { | ||
| if (!Object.prototype.hasOwnProperty.call(b, key)) return false; | ||
| if (!compareValues(a[key], b[key])) 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,6 @@ | ||
| export const deepEquals = (a: unknown, b: unknown) => { | ||
| return a === b; | ||
| import { baseEquals } from "./baseEquals"; | ||
|
|
||
| export const deepEquals = (a: unknown, b: unknown): boolean => { | ||
| // baseEquals를 사용하여 공통 로직 처리, 값 비교는 깊은 비교(재귀 호출) | ||
| return baseEquals(a, b, (valueA, valueB) => deepEquals(valueA, valueB)); | ||
| }; |
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,2 +1,3 @@ | ||
| export * from "./baseEquals"; | ||
| export * from "./shallowEquals"; | ||
| export * from "./deepEquals"; |
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,5 @@ | ||
| import { baseEquals } from "./baseEquals"; | ||
|
|
||
| export const shallowEquals = (a: unknown, b: unknown) => { | ||
| return a === b; | ||
| return baseEquals(a, b, (valueA, valueB) => valueA === valueB); | ||
| }; |
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,8 @@ | ||
| import type { FunctionComponent } from "react"; | ||
| import { memo } from "./memo"; | ||
| import { deepEquals } from "../equals"; | ||
|
|
||
| // deepMemo HOC는 컴포넌트의 props를 깊은 비교하여 불필요한 리렌더링을 방지합니다. | ||
| export function deepMemo<P extends object>(Component: FunctionComponent<P>) { | ||
| return Component; | ||
| return memo(Component, deepEquals); | ||
| } |
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,23 @@ | ||
| import { type FunctionComponent } from "react"; | ||
| import { shallowEquals } from "../equals"; | ||
| import { useRef } from "../hooks/useRef"; | ||
|
|
||
| export function memo<P extends object>(Component: FunctionComponent<P>, equals = shallowEquals) { | ||
| return Component; | ||
| return function MemoizedComponent(props: P) { | ||
| // 1. 이전 props와 결과 저장 (커스텀 useRef 사용) | ||
| const prevPropsRef = useRef<P | null>(null); | ||
| const prevResultRef = useRef<ReturnType<typeof Component> | null>(null); | ||
|
|
||
| // 2. props가 변경됐는지 비교 | ||
| const isSame = prevPropsRef.current !== null && equals(prevPropsRef.current, props); | ||
|
|
||
| if (!isSame) { | ||
| // 3. 변경된 경우: 새로 렌더링 | ||
| prevPropsRef.current = props; | ||
| prevResultRef.current = Component(props); | ||
| } | ||
|
|
||
| // 4. 변경되지 않은 경우: 이전 결과 재사용 | ||
| return prevResultRef.current; | ||
| }; | ||
| } |
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,16 @@ | ||
| /* eslint-disable @typescript-eslint/no-unused-vars,@typescript-eslint/no-unsafe-function-type */ | ||
| import type { DependencyList } from "react"; | ||
| import { useRef } from "./useRef"; | ||
| import { shallowEquals } from "../equals"; | ||
|
|
||
| export function useCallback<T extends Function>(factory: T, _deps: DependencyList) { | ||
| // 직접 작성한 useMemo를 통해서 만들어보세요. | ||
| return factory as T; | ||
| export function useCallback<T>(factory: T, deps: DependencyList): T { | ||
| const callbackRef = useRef<T>(factory); | ||
| const depsRef = useRef<DependencyList>(deps); | ||
|
|
||
| // 의존성이 변경되었는지 확인 | ||
| if (!shallowEquals(deps, depsRef.current)) { | ||
| callbackRef.current = factory; | ||
| depsRef.current = deps; | ||
| } | ||
|
|
||
| return callbackRef.current; | ||
| } |
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,36 @@ | ||
| /* 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(); | ||
| type MemoState<T> = { deps: DependencyList | undefined; value: T } | null; | ||
| type EqualsFn = (a: unknown, b: unknown) => boolean; | ||
| type Factory<T> = () => T; | ||
|
|
||
| const isFirstRender = <T>(state: MemoState<T>): boolean => state === null; | ||
|
|
||
| const hasDepsChanged = <T>(state: MemoState<T>, newDeps: DependencyList, equals: EqualsFn): boolean => | ||
| state !== null && !equals(state.deps, newDeps); | ||
|
|
||
| const shouldRecalculate = <T>(state: MemoState<T>, deps: DependencyList, equals: EqualsFn): boolean => | ||
| isFirstRender(state) || hasDepsChanged(state, deps, equals); | ||
|
|
||
| const createMemoState = <T>(deps: DependencyList, value: T): MemoState<T> => ({ | ||
| deps, | ||
| value, | ||
| }); | ||
|
|
||
| const calculateAndCache = <T>(factory: Factory<T>, deps: DependencyList, stateRef: { current: MemoState<T> }): T => { | ||
| const value = factory(); | ||
| stateRef.current = createMemoState(deps, value); | ||
| return value; | ||
| }; | ||
|
|
||
| const getCachedValue = <T>(state: MemoState<T>): T => state!.value; | ||
|
|
||
| export function useMemo<T>(factory: Factory<T>, deps: DependencyList, equals: EqualsFn = shallowEquals): T { | ||
| const memoRef = useRef<MemoState<T>>(null); | ||
|
|
||
| return shouldRecalculate(memoRef.current, deps, equals) | ||
| ? calculateAndCache(factory, deps, memoRef) | ||
| : getCachedValue(memoRef.current); | ||
| } |
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,4 +1,7 @@ | ||
| import { useState } from "react"; | ||
|
|
||
| export function useRef<T>(initialValue: T): { current: T } { | ||
| // useState를 이용해서 만들어보세요. | ||
| return { current: initialValue }; | ||
| const [ref] = useState(() => ({ current: initialValue })); | ||
|
|
||
| return ref; | ||
| } |
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,9 +1,21 @@ | ||
| import { useRef } from "react"; | ||
| import { useRef } from "./useRef"; | ||
| import { shallowEquals } from "../equals"; | ||
|
|
||
| type Selector<T, S = T> = (state: T) => S; | ||
|
|
||
| export const useShallowSelector = <T, S = T>(selector: Selector<T, S>) => { | ||
| // 이전 상태를 저장하고, shallowEquals를 사용하여 상태가 변경되었는지 확인하는 훅을 구현합니다. | ||
| return (state: T): S => selector(state); | ||
| const prevValueRef = useRef<S | null>(null); | ||
|
|
||
| return (state: T): S => { | ||
| const newValue = selector(state); | ||
|
|
||
| // 이전 값과 비교하여 변경되었는지 확인 | ||
| if (prevValueRef.current !== null && shallowEquals(prevValueRef.current, newValue)) { | ||
| return prevValueRef.current; | ||
| } | ||
|
|
||
| // 값이 변경되었으면 새로운 값으로 업데이트 | ||
| prevValueRef.current = newValue; | ||
| return newValue; | ||
| }; | ||
| }; |
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.
같은 코드베이스 반복되지 않도록 빼두신거 넘 좋은 것 같습니다 👍