Skip to content

Instantly share code, notes, and snippets.

@artalar
Created November 9, 2021 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artalar/8face47d9062c5fdcd0f8db0229c7c87 to your computer and use it in GitHub Desktop.
Save artalar/8face47d9062c5fdcd0f8db0229c7c87 to your computer and use it in GitHub Desktop.
Pseudocode that's demonstrate how react hooks work under hood
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