When is it appropriate to use a constructor in REACT?

If you don’t initialize state and you don’t bind methods, you don’t need to implement a constructor for your React component. The constructor for a React component is called before it is mounted. When implementing the constructor for a React.Component subclass, you should call super(props) before any other statement. Otherwise, this.props will be undefined in … Read more

Extend Javascript promise and resolve or reject it inside constructor

The reasoning is simple but not necessarily self evident. .then() returns a promise if then is called on a subclass of Promise, the returned promise is an instance of the subclass, not Promise itself. the then returned promise is constructed by calling the subclass constructor, and passing it an internal executor function that records the … Read more

How to clone a javascript ES6 class instance

It is complicated; I tried a lot! In the end, this one-liner worked for my custom ES6 class instances: let clone = Object.assign(Object.create(Object.getPrototypeOf(orig)), orig) It avoids setting the prototype because they say it slows down the code a lot. It supports symbols but isn’t perfect for getters/setters and isn’t working with non-enumerable properties (see Object.assign() … Read more

Call static methods from regular ES6 class methods

Both ways are viable, but they do different things when it comes to inheritance with an overridden static method. Choose the one whose behavior you expect: class Super { static whoami() { return “Super”; } lognameA() { console.log(Super.whoami()); } lognameB() { console.log(this.constructor.whoami()); } } class Sub extends Super { static whoami() { return “Sub”; } … Read more