3 ms·
Not specific to React, but with CommonJS modules you can use module scope instead of global scope to get private methods and variables, which the function can a
by z3t4 8y ago
Not specific to React, but with CommonJS modules you can use module scope instead of global scope to get private methods and variables, which the function can access via the closure.
var foo = 1;
var bar = x => x++;
module.exports = function baz() { return bar(foo) }
I find this much more simple then classes. I even consider it an anti-pattern to make a class for something this simple.
- crooked-v 8y agoThat gets you a single instance, but the whole point of React components is that you can use them lots of time and each will do its own instance of the thing.
- pault 8y agoThe next dev that comes along and naively imports your module into another part of the app will waste their time trying to figure out why foo keeps getting overwritten by the second instance.