Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 code styles best practices

Recently I've started to learn ReactJS and consequently - ES6. I'm quite familiar with ES5, but some things are not that clear for me.

Example 1: Methods syntax

What is the difference between the following two methods?

export class InvoiceForm extends React.Component {
    methodName1() {
    }

    methodName2 = () => {

    };
}

Example 2: Class properties outside

class Greeting extends React.Component {
  render() {
    return (
      <h1>Hello, {this.props.name}</h1>
    );
  }
}

Greeting.propTypes = {
  name: PropTypes.string
};

propTypes is outside the class. But why? I came from python and as for me, the following is more correct

class Greeting extends React.Component {
  static propTypes = {
    name: PropTypes.string
  }
  render() {
    return (
      <h1>Hello, {this.props.name}</h1>
    );
  }
}
like image 623
Vadym Avatar asked Dec 03 '17 21:12

Vadym


People also ask

What is ES6 style?

JavaScript ES6 brings new syntax and new awesome features to make your code more modern and more readable. It allows you to write less code and do more. ES6 introduces us to many great features like arrow functions, template strings, class destruction, Modules… and more.

Should I use camelCase in JavaScript?

camelCase is used by JavaScript itself, by jQuery, and other JavaScript libraries. Do not start names with a $ sign. It will put you in conflict with many JavaScript library names.

Does ES6 support types?

ES6 does not support all data types. TypeScript contains features such as generics and type annotations, Inference, Enums, and Interfaces. ES6 does not support these features.

What are the most important features of ES6?

This chapter describes the most important features of ES6. The let keyword allows you to declare a variable with block scope. Read more about let in the chapter: JavaScript Let. The const keyword allows you to declare a constant (a JavaScript variable with a constant value).

What's new in ES6 syntax?

If you are rewriting legacy code using ES6 syntax, this is something to watch out for. ES6 adds a way to iterate over each of the values in an array. This is different from the existing for ... in loop that loops over the key/index.

What is the difference between includes () and rest () methods in ES6?

ES6 allows function parameters to have default values. The rest parameter (...) allows a function to treat an indefinite number of arguments as an array: The includes () method returns true if a string contains a specified value, otherwise false: let text = "Hello world, welcome to the universe.";

What is ES6 (ECMAScript 6)?

ECMAScript 6, also known as ES6 and ECMAScript 2015, was the second major revision to JavaScript. This chapter describes the most important features of ES6. The let keyword allows you to declare a variable with block scope.


Video Answer


2 Answers

What is the difference between the following two methods?

The first one is prototype method (this.__proto__.methodName1) which is not bound to this context and valid in ES6. The second one is instance method (this.methodName1) which is bound to this context and a part of a proposal.

propTypes is outside the class. But why?

Because class fields aren't supported in ES6. Since the example uses JSX and is supposed to be built with Babel any way, it makes sense to use ES.next features and static propTypes = ... field.

like image 153
Estus Flask Avatar answered Oct 17 '22 05:10

Estus Flask


What is the difference between the following two methods?

 methodName1() {   }

above is a normal function and this keyword in this function refers to the context of the function itself.

So if you try to access React class Properties/functions like this.setState you will get an error (if you haven't used binding anywhere for methodName1 like :

this.methodName1 = this.methondName1.bind(this) prefarbaly you want to do it inside constructor method.

If you want to learn more about this binding you can see this Article

However In the second methodName2 syntax, function is written using Arrow function syntax.

 methodName2 = () => {
    };

An arrow function does not have its own this , arguments, super or new.target. Hence this keyword inside this function will refer to the context of the React class (React.Component) as described Here

And regarding your second question

Class properties outside

I believe as it uses JSX , and JSX is supported by Babel and ES6 will almost certainly not cover syntax for defining class variables. You can read more it Here

like image 38
Aaqib Avatar answered Oct 17 '22 03:10

Aaqib