Skip to content

Instantly share code, notes, and snippets.

@rndme
Created September 3, 2019 20:50
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 rndme/b7aabbbf3adbd841a750171f37e5e285 to your computer and use it in GitHub Desktop.
Save rndme/b7aabbbf3adbd841a750171f37e5e285 to your computer and use it in GitHub Desktop.
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