So as per convention since JavaScript doesn't have access modifiers I have to prefix "private" function names with an underscore(_). But I am a little confused here, say I am writing a class in C++ or Java that has two functions one that performs internal calculations and the second that returns the result of that internal calculation.
So, I have these two functions
performInternalSecretCalculation();
getResult();
the performInternalSecretCalculation()
is supposed to be private since I don't want other classes to worry about how the class handles calculation and hence I don't export this function. Whereas getResult()
is something that will be used by other classes and hence I make it public and export this function.
But in case of React classes I am not exporting any functions all the functions defined inside a react class are used within it. So what is the differentiator? When should I prefix an underscore before a function's name?
And React components must be uppercased In JSX, so an uppercase filename keeps the component name and filename in sync.
In short, with arrow functions there is no binding of this . In regular functions the this keyword represented the object that called the function, which could be the window, the document, a button or whatever. With arrow functions, the this keyword always represents the object that defined the arrow function.
In React applications, there are 3 most common naming conventions: Camel case for file names, and pascal case for component names. Kebab case for file names, and pascal case for component names. Pascal case for both file names, and component names.
TitleCase for component files is best because it allows you to know other camelCase files are exporting something else than a component.
It is just a naming convention, used by some developers for internal methods to separate them from the lifecycle methods of react.
List is borrowed from the AirBnB Style Guide.
I prefer the Style Guide from AirBnB. They suggest not to use underscore prefix. I force instead a clean order for the methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With