Skip to content

Instantly share code, notes, and snippets.

@rofrol
Last active April 19, 2020 19:52
Show Gist options
  • Save rofrol/2ac749eab2fe30782701238b04bcd682 to your computer and use it in GitHub Desktop.
Save rofrol/2ac749eab2fe30782701238b04bcd682 to your computer and use it in GitHub Desktop.
// https://dmitripavlutin.com/react-hooks-stale-closures/
function createIncrementFixed(i) {
let value = 0;
function increment() {
value += i;
console.log(value);
return function logValue() {
const message = `Current value is ${value}`;
console.log(message);
};
}
return increment;
}
const inc = createIncrementFixed(1);
const log = inc(); // logs 1
inc(); // logs 2
inc(); // logs 3
// Works!
log(); // logs "Current value is 3"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment