-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: equalities / memo / useCallback 등등 기본 구현 추가 (기본과제)
- Loading branch information
Showing
11 changed files
with
2,658 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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 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,44 @@ | ||
export function deepEquals<T>(objA: T, objB: T): boolean { | ||
return objA === objB; | ||
if (typeof objA !== typeof objB) { | ||
return false; | ||
} | ||
|
||
if (objA == null && objB == null) { | ||
return objA === objB; | ||
} | ||
|
||
if (typeof objA !== "object" && typeof objB !== "object") { | ||
return objA === objB; | ||
} | ||
|
||
if (Array.isArray(objA) !== Array.isArray(objB)) { | ||
return false; | ||
} | ||
|
||
if (Array.isArray(objA) && Array.isArray(objB)) { | ||
if (objA.length !== objB.length) { | ||
return false; | ||
} | ||
|
||
return objA.every((value, index) => deepEquals(value, objB[index])); | ||
} | ||
|
||
const keysA = Object.keys(objA as object); | ||
const keysB = Object.keys(objB as object); | ||
|
||
if (keysA.length !== keysB.length) { | ||
return false; | ||
} | ||
|
||
if (keysA.some((key) => !keysB.includes(key))) { | ||
return false; | ||
} | ||
|
||
for (const key in objA) { | ||
if (!deepEquals(objA[key], objB[key])) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} |
This file contains 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,33 @@ | ||
export function shallowEquals<T>(objA: T, objB: T): boolean { | ||
return objA === objB; | ||
if (objA === objB) { | ||
return true; | ||
} | ||
|
||
if ( | ||
typeof objA !== "object" || | ||
objA == null || | ||
typeof objB !== "object" || | ||
objB == null | ||
) { | ||
return false; | ||
} | ||
|
||
const keysA = Object.keys(objA); | ||
const keysB = Object.keys(objB); | ||
|
||
if (keysA.length !== keysB.length) { | ||
return false; | ||
} | ||
|
||
if (keysA.some((key) => !keysB.includes(key))) { | ||
return false; | ||
} | ||
|
||
for (const key in objA) { | ||
if (objA[key] !== objB[key]) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} |
This file contains 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,7 @@ | ||
import { deepEquals } from "../equalities"; | ||
import { ComponentType } from "react"; | ||
import { memo } from "./memo.ts"; | ||
import { memo } from "./memo.tsx"; | ||
|
||
export function deepMemo<P extends object>(Component: ComponentType<P>) { | ||
return memo(Component, deepEquals); | ||
export function deepMemo<P extends object, R>(Component: ComponentType<P>) { | ||
return memo<P, R>(Component, deepEquals); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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,19 @@ | ||
import { shallowEquals } from "../equalities"; | ||
import { ComponentType } from "react"; | ||
|
||
export function memo<P extends object, R>( | ||
Component: ComponentType<P>, | ||
_equals = shallowEquals, | ||
): (props: P) => R { | ||
const prevProps = { current: { value: {} } as { value: P } }; | ||
let memoizedResult: R; | ||
|
||
return (props: P) => { | ||
if (!_equals(props, prevProps.current.value)) { | ||
prevProps.current.value = props; | ||
memoizedResult = (<Component {...props} />) as R; | ||
} | ||
|
||
return memoizedResult; | ||
}; | ||
} |
This file contains 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,10 +1,10 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars,@typescript-eslint/no-unsafe-function-type */ | ||
/* eslint-disable @typescript-eslint/no-unsafe-function-type */ | ||
import { DependencyList } from "react"; | ||
import { useMemo } from "./useMemo"; | ||
|
||
export function useCallback<T extends Function>( | ||
factory: T, | ||
_deps: DependencyList, | ||
) { | ||
// 직접 작성한 useMemo를 통해서 만들어보세요. | ||
return factory as T; | ||
return useMemo(() => factory, _deps); | ||
} |
This file contains 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 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,12 +1,22 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { DependencyList } from "react"; | ||
import { shallowEquals } from "../equalities"; | ||
import { useRef } from "./useRef"; | ||
|
||
export function useMemo<T>( | ||
factory: () => T, | ||
_deps: DependencyList, | ||
_equals = shallowEquals, | ||
): T { | ||
// 직접 작성한 useRef를 통해서 만들어보세요. | ||
return factory(); | ||
const ref = useRef<{ deps: DependencyList; value: T } | null>(null); | ||
|
||
if (ref.current == null) { | ||
ref.current = { deps: _deps, value: factory() }; | ||
return ref.current.value; | ||
} | ||
|
||
if (!_equals(_deps, ref.current.deps)) { | ||
ref.current = { deps: _deps, value: factory() }; | ||
} | ||
|
||
return ref.current.value; | ||
} |
This file contains 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 } { | ||
// React의 useState를 이용해서 만들어보세요. | ||
return { current: initialValue }; | ||
const [value] = useState<{ current: T }>({ current: initialValue }); | ||
|
||
return value; | ||
} |
Oops, something went wrong.