Created
September 3, 2019 20:50
-
-
Save rndme/b7aabbbf3adbd841a750171f37e5e285 to your computer and use it in GitHub Desktop.
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(value){ // designed after https://reactjs.org/docs/hooks-intro.html demo | |
var o = useState.state || (useState.state = new Map()); | |
if(o.has(arguments.callee.caller)){ // not first time? | |
return o.get(arguments.callee.caller); | |
}else{ | |
// build array of returns, set it, return it | |
var ret = []; | |
[...arguments].map((x, i)=>{ | |
ret.push(x) | |
ret.push(function _updater(val){ ret[(i*2)] = val; }); | |
}); | |
o.set(arguments.callee.caller,ret); | |
return ret; | |
}//end if first Time calling? | |
}//end useState | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment