Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
'use strict';
const stateMap = new WeakMap();
function useState(key, init) {
const update = (v) => stateMap.set(key, v);
if (stateMap.has(key)) {
return [stateMap.get(key), update];
}
stateMap.set(key, init);
return [init, update];
}
for (let i = 0; i < 10; i += 1) {
const [count, setCount] = useState`${0}`;
console.log(count);
setCount(count + 1);
}
@cmoog

This comment has been minimized.

Copy link

cmoog commented Jun 7, 2019

@devsnek Very clever use of the array reference as a key in the WeakMap. Would there be any way to associate this hook with its surrounding component and trigger a rerender when necessary?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.