Created
April 13, 2024 16:59
-
-
Save pshaddel/ad2f1d366f2869f741fce87e17bb466c to your computer and use it in GitHub Desktop.
Use State Wrong Implementation
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
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