Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of prefixing function names with '_' React/React-native ?

Tags:

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?

like image 430
Nash Vail Avatar asked Dec 16 '15 07:12

Nash Vail


People also ask

Should React component file names be capitalized?

And React components must be uppercased In JSX, so an uppercase filename keeps the component name and filename in sync.

What is the use of arrow function in react native?

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.

What is the naming convention in React?

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.

Should component files be capitalized?

TitleCase for component files is best because it allows you to know other camelCase files are exporting something else than a component.


1 Answers

It is just a naming convention, used by some developers for internal methods to separate them from the lifecycle methods of react.

Lifecycle methods

  • constructor
  • getChildContext
  • componentWillMount
  • componentDidMount
  • componentWillReceiveProps
  • shouldComponentUpdate
  • componentWillUpdate
  • componentDidUpdate
  • componentWillUnmount

List is borrowed from the AirBnB Style Guide.

Airbnb React/JSX 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.

like image 103
purii Avatar answered Sep 23 '22 12:09

purii