Created
November 9, 2021 23:08
-
-
Save artalar/8face47d9062c5fdcd0f8db0229c7c87 to your computer and use it in GitHub Desktop.
Pseudocode that's demonstrate how react hooks work under hood
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
let currentComponent; | |
let currentMemory; | |
let currentIndex; | |
function useState(initState) { | |
if (currentIndex in currentMemory === false) { | |
currentMemory.push(initState); | |
} | |
const state = currentMemory[currentIndex]; | |
currentIndex++; | |
const myComponent = currentComponent; | |
return [ | |
state, | |
(newState) => { | |
state = newState; | |
render(myComponent); | |
}, | |
]; | |
} | |
function render(Component) { | |
currentComponent = Component; | |
currentMemory = []; | |
currentIndex = 0; | |
const element = Component(); | |
renderToDOM(element); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment