-
Notifications
You must be signed in to change notification settings - Fork 0
[Seoin] React Hooks #2
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
Seoin02
wants to merge
13
commits into
seoin
Choose a base branch
from
seoin_dev
base: seoin
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
13 commits
Select commit
Hold shift + click to select a range
daa5e25
📝 Docs: add readme.md
D5ng 235bd57
📝 Docs: readme.md update
D5ng c7930ce
chore: 프리티어 세팅
Seoin02 20c50ca
feat: 전역 타입 Window 선언
Seoin02 61ae864
feat: useState 구현
Seoin02 1e5a475
chore: decrement 함수 수정
Seoin02 b6e92de
refactor: 파일 분리, 모듈화
Seoin02 8dd05ce
Merge branch 'main' into seoin_dev
Seoin02 e263b5b
feat: useEffect hook 구현
Seoin02 5904c42
Merge branch 'seoin_dev' of https://github.com/React-Core-Learn/React…
Seoin02 369efbd
feat: useCallback hook 구현
Seoin02 0a8d5f9
feat: useMemo hook 구현
Seoin02 e579eaa
refactor: 각 hook마다 state를 따로 관리하게 수정
Seoin02 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 |
|---|---|---|
|
|
@@ -23,4 +23,5 @@ dist-ssr | |
| *.sln | ||
| *.sw? | ||
|
|
||
| .yarn | ||
| .yarn | ||
| yarn.lock | ||
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,13 @@ | ||
| { | ||
| "arrowParens": "avoid", | ||
| "bracketSpacing": true, | ||
| "bracketSameLine": true, | ||
| "jsxBracketSameLine": true, | ||
| "jsxSingleQuote": false, | ||
| "printWidth": 120, | ||
| "semi": true, | ||
| "singleQuote": true, | ||
| "tabWidth": 2, | ||
| "trailingComma": "es5", | ||
| "endOfLine": "auto" | ||
| } |
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,8 @@ | ||
| declare global { | ||
| interface Window { | ||
| increment: () => void; | ||
| decrement: () => void; | ||
| } | ||
| } | ||
|
|
||
| export {}; |
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,9 @@ | ||
| import CounterAndMeow from './components/CounterAndMeow'; | ||
|
|
||
| const App = () => ` | ||
| <div> | ||
| ${CounterAndMeow()} | ||
| </div> | ||
| `; | ||
|
|
||
| export default App; |
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,26 @@ | ||
| import MyReact from '../core/MyReact'; | ||
|
|
||
| const { useState, render } = MyReact(); | ||
|
|
||
| export default function CounterAndMeow() { | ||
| const [count, setCount] = useState(1); | ||
| const [cat, setCat] = useState('야옹!'); | ||
|
|
||
| function countMeow(newCount: number) { | ||
| setCount(newCount); | ||
| setCat('야옹! '.repeat(newCount)); | ||
| } | ||
|
|
||
| window.increment = () => countMeow(count + 1); | ||
| window.decrement = () => { | ||
| if (count > 0) countMeow(count - 1); | ||
| }; | ||
|
|
||
| return ` | ||
| <div> | ||
| <p>고양이가 ${count}번 울어서 ${cat} </p> | ||
| <button onclick="increment()">증가</button> | ||
| <button onclick="decrement()">감소</button> | ||
| </div> | ||
| `; | ||
| } |
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,140 @@ | ||
| import debounceFrame from '../utils/debounceFrame'; | ||
|
|
||
| interface Options { | ||
| renderCount: number; | ||
| componentStateMap: Map<any, { states: any[]; currentStateKey: number }>; | ||
| root: Element | null; | ||
| rootComponent: any; | ||
| } | ||
|
|
||
| export default function MyReact() { | ||
| const options: Options = { | ||
| renderCount: 0, | ||
| componentStateMap: new Map(), | ||
| root: null, | ||
| rootComponent: null, | ||
| }; | ||
|
|
||
| function getComponentState(component: any) { | ||
| if (!options.componentStateMap.has(component)) { | ||
| options.componentStateMap.set(component, { states: [], currentStateKey: 0 }); | ||
| } | ||
| return options.componentStateMap.get(component)!; | ||
| } | ||
|
|
||
| function useState(initState: any) { | ||
| const component = options.rootComponent; | ||
| const componentState = getComponentState(component); | ||
| const { currentStateKey: key, states } = componentState; | ||
|
|
||
| if (states.length === key) states.push(initState); | ||
|
|
||
| const state = states[key]; | ||
| const setState = (newState: any) => { | ||
| states[key] = newState; | ||
| _render(); | ||
| }; | ||
|
|
||
| componentState.currentStateKey += 1; | ||
| return [state, setState]; | ||
| } | ||
|
|
||
| const _render = debounceFrame(() => { | ||
| const { root, rootComponent } = options; | ||
| if (!root || !rootComponent) return; | ||
| root.innerHTML = rootComponent(); | ||
|
|
||
| options.componentStateMap.forEach(state => (state.currentStateKey = 0)); | ||
|
|
||
| options.renderCount += 1; | ||
| }); | ||
|
|
||
| function useEffect<T extends (...arg: any[]) => any>(callback: T, dependencies: any[]) { | ||
| const component = options.rootComponent; | ||
| const componentState = getComponentState(component); | ||
| const { currentStateKey: key, states } = componentState; | ||
|
|
||
| const oldDependencies = states[key]; | ||
| let hasChanged = true; | ||
|
|
||
| if (oldDependencies) { | ||
| hasChanged = dependencies.some((dep, index) => !Object.is(dep, oldDependencies[index])); | ||
| } | ||
| if (hasChanged) { | ||
| callback(); | ||
| states[key] = dependencies; | ||
| } | ||
|
|
||
| componentState.currentStateKey += 1; | ||
| } | ||
|
|
||
| function useCallback<T extends (...arg: any[]) => any>(callback: T, dependencies: any[]) { | ||
| const component = options.rootComponent; | ||
| const componentState = getComponentState(component); | ||
| const { currentStateKey: key, states } = componentState; | ||
|
|
||
| const oldDependencies = states[key]?.dependencies; | ||
| let hasChanged = true; | ||
|
|
||
| if (oldDependencies) { | ||
| hasChanged = | ||
| dependencies.length !== oldDependencies.length || | ||
| dependencies.some((dep, index) => !Object.is(dep, oldDependencies[index])); | ||
| } | ||
|
|
||
| if (hasChanged) { | ||
| states[key] = { callback, dependencies }; | ||
| } | ||
|
|
||
| componentState.currentStateKey += 1; | ||
| return states[key].callback; | ||
| } | ||
|
|
||
| function useMemo<T>(callback: () => T, dependencies: any[]) { | ||
| const component = options.rootComponent; | ||
| const componentState = getComponentState(component); | ||
| const { currentStateKey: key, states } = componentState; | ||
|
|
||
| const [oldDependencies, oldMemoValue] = states[key] || []; | ||
| let hasChanged = true; | ||
| let memoValue = oldMemoValue; | ||
|
|
||
| if (oldDependencies) { | ||
| hasChanged = dependencies.some((dep, index) => !Object.is(dep, oldDependencies[index])); | ||
| } | ||
|
|
||
| if (hasChanged) { | ||
| memoValue = callback(); | ||
| states[key] = [dependencies, memoValue]; | ||
| } | ||
|
|
||
| componentState.currentStateKey += 1; | ||
| return memoValue; | ||
| } | ||
|
|
||
| function render(rootComponent: any, root: Element | null) { | ||
| options.root = root; | ||
| options.rootComponent = rootComponent; | ||
| _render(); | ||
| } | ||
|
|
||
| return { useState, useEffect, useCallback, useMemo, render }; | ||
| } | ||
|
|
||
| // state를 외부에서 관리하지 않아 계속 초기화 문제 발생 | ||
| // function useCallback<T extends (...arg: any[]) => any>(fn: T, dependencies: any[]) { | ||
| // let cachedFn: T | null = null; | ||
| // let cachedDependencies: any[] | null = null; | ||
|
|
||
| // const hasChanged = | ||
| // cachedDependencies === null || | ||
| // dependencies.length !== cachedDependencies.length || | ||
| // dependencies.some((dependency, index) => !Object.is(dependency, cachedDependencies[index])); | ||
|
|
||
| // if (hasChanged) { | ||
| // cachedFn = fn; | ||
| // cachedDependencies = dependencies; | ||
| // } | ||
|
|
||
| // return cachedFn || fn; | ||
| // } | ||
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 +1,5 @@ | ||
| // Your code.. | ||
| import MyReact from './core/MyReact'; | ||
| import App from './App'; | ||
|
|
||
| const { render } = MyReact(); | ||
| render(App, document.querySelector('#app')); |
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,7 @@ | ||
| export default function debounceFrame(callback: FrameRequestCallback) { | ||
| let nextFrameCallback = -1; | ||
| return () => { | ||
| cancelAnimationFrame(nextFrameCallback); | ||
| nextFrameCallback = requestAnimationFrame(callback); | ||
| }; | ||
| } |
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
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.
getComponentState라는 네이밍도 좋고, Map을 사용한 것도 좋아보이네요.하나 궁금한건 component를 매개변수로 받는데, 이 컴포넌트는 결국 루트 컴포넌트 아닌가요 ????
루트 컴포넌트가 아닌 커스텀 컴포넌트들도 받는건지 궁금합니다