Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pshaddel/ad2f1d366f2869f741fce87e17bb466c to your computer and use it in GitHub Desktop.
Save pshaddel/ad2f1d366f2869f741fce87e17bb466c to your computer and use it in GitHub Desktop.
Use State Wrong Implementation
function useState(initialValue) {
var _state = initialValue // private value
function state() {
return _state
}
function setState(newVal) {
_state = newVal
}
return [_state, setState] // exposing functions for external use
};
const [count, setCount] = useState(1);
console.log(count); // 1
setCount(2);
console.log(count); // 1 !!!!! Why???
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment