Skip to content

Instantly share code, notes, and snippets.

@oslego
Forked from WebReflection/hooks.js
Last active December 4, 2020 18:20
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 oslego/ff8aa52c3eff8afd3b075e989498defa to your computer and use it in GitHub Desktop.
Save oslego/ff8aa52c3eff8afd3b075e989498defa to your computer and use it in GitHub Desktop.
const augment = callback => () => {
const useState = {
init: true,
value: void 0,
update(value) {
useState.value = value;
callback(hooks)
}
};
const hooks = {
useState(value) {
if (useState.init) {
useState.init = false;
useState.value = value;
}
return [useState.value, useState.update];
}
};
callback(hooks);
};
const counter = augment(({useState}) => {
const [i, update] = useState(0);
console.log(i);
setTimeout(update, 1000, i + 1);
});
// once invoked, it should log the `i` from 0 to N
// each second the `i` should be incremented + 1
counter();
// 0
// 1
// 2
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment