You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A bit. The thing about React components is that all methods should be private so we just save ourselves the typing and don't insert the leading underscore.
What do you mean? If I have e.g. a ref to an element, I can call methods on it just fine. I've seen this used to implement e.g. getValue methods on things like complex, uncontrolled input components for forms.
If you consider this an anti-pattern, it could be useful to add a clarifying comment to say to not do this. There are definitely problems with this pattern - it's not container-safe, for one.
Personally, I think this pattern is horrifically bad. Things that are not private are not private - sticking an underscore in front of them doesn't make them private, it just lulls you into the false belief that you can make a breaking change without bumping a major version.
It's not private if it's accessible in the runtime. If it's private - ie, via closure or WeakMap - no convention or unit tests are needed. If it's not, it's public, and you must test it, and whether you document it or not, it is public and breaking it counts as a breaking change. At that point, using a leading underscore is just fluff, so why would you need a convention for it?
donavon, stianlik, track0x1, nightwolfz, dash- and 10 morebramblex, skykit-karlkroening, minomikula, jeremyBanks and tonci-bw
We should probably just remove this section from the main style guide or rephrase it. Same with the React style guide. This whole attachment to "private" is what makes the rules seem confusing.
Activity
goatslacker commentedon Aug 28, 2015
A bit. The thing about React components is that all methods should be private so we just save ourselves the typing and don't insert the leading underscore.
taion commentedon Aug 28, 2015
What do you mean? If I have e.g. a ref to an element, I can call methods on it just fine. I've seen this used to implement e.g.
getValue
methods on things like complex, uncontrolled input components for forms.If you consider this an anti-pattern, it could be useful to add a clarifying comment to say to not do this. There are definitely problems with this pattern - it's not container-safe, for one.
afilp commentedon Oct 25, 2015
I am also interested in an explanation to that. Not all methods are "internal" if we consider that we may call a component's method from the parent.
Example:
parent.jsx
childComponent.jsx
So, what is our general take on this? Do we still write all methods without any underscore?
taion commentedon Oct 25, 2015
👍 Facebook, for example, use this pattern extensively: https://github.com/facebook/relay/blob/v0.4.0/src/container/RelayRootContainer.js
hbrls commentedon Oct 26, 2015
@taion what about v0.14
taion commentedon Oct 26, 2015
What about it?
ljharb commentedon Oct 26, 2015
Personally, I think this pattern is horrifically bad. Things that are not private are not private - sticking an underscore in front of them doesn't make them private, it just lulls you into the false belief that you can make a breaking change without bumping a major version.
It's not private if it's accessible in the runtime. If it's private - ie, via closure or WeakMap - no convention or unit tests are needed. If it's not, it's public, and you must test it, and whether you document it or not, it is public and breaking it counts as a breaking change. At that point, using a leading underscore is just fluff, so why would you need a convention for it?
taion commentedon Oct 26, 2015
@ljharb
That's totally fine - I personally don't mind writing my code either way (as long as I can point to a style guide and say that I'm following it).
The issue is that this style guide appears to be inconsistent between https://github.com/airbnb/javascript#22.4 and https://github.com/airbnb/javascript/tree/master/react#methods. I don't see how it's possible to follow both.
ljharb commentedon Oct 26, 2015
React is a special subset - you could follow both by using
_
-prefixes in non-React code. I don't see a logical disconnect there.taion commentedon Oct 26, 2015
Okay. If that's intentional, I can live with it.
ljharb commentedon Apr 12, 2016
JS doesn't have privacy. Underscore-prefixed things are just as public as anything else.
goatslacker commentedon Apr 12, 2016
We should probably just remove this section from the main style guide or rephrase it. Same with the React style guide. This whole attachment to "private" is what makes the rules seem confusing.
46 remaining items