Skip to content

Instantly share code, notes, and snippets.

@vishnuroshan
Created June 9, 2023 12:50
Show Gist options
  • Save vishnuroshan/b2828917986ee3575fe4ece84989c99a to your computer and use it in GitHub Desktop.
Save vishnuroshan/b2828917986ee3575fe4ece84989c99a to your computer and use it in GitHub Desktop.
A very crude imagination of how useState works internally in react (definitely wrong)
function useState(initVal) {
let _val = initVal;
let state = () => _val;
const setState = (newVal) => _val = newVal;
return [state, setState]
}
const [c, setC] = useState(1);
console.log(c());
setC(2);
console.log(c());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment