How can I prevent my functional component from re-rendering with React memo or React hooks?

As G.aziz said, React.memo functions similarly to pure component. However, you can also adjust its behavior by passing it a function which defines what counts as equal. Basically, this function is shouldComponentUpdate, except you return true if you want it to not render. const areEqual = (prevProps, nextProps) => true; const MyComponent = React.memo(props => … Read more

How do I write an arrow function in ES6 recursively?

Writing a recursive function without naming it is a problem that is as old as computer science itself (even older, actually, since λ-calculus predates computer science), since in λ-calculus all functions are anonymous, and yet you still need recursion. The solution is to use a fixpoint combinator, usually the Y combinator. This looks something like … Read more

Export (default) class in ReactJS

Hi and welcome to React! I think part of what you’re having trouble with here is not really React-specific, but instead related to the new ES2015 module syntax. When creating React class components, for most intents and purposes you can think of React.createClass as functionally equivalent to class MyComponent extends React.Component. One is just using … Read more

ES6 Class: access to ‘this’ with ‘addEventListener’ applied on method [duplicate]

This is a general JS issue, but the core of it is that this.elm.addEventListener(‘click’, this.sayHello); is no different than var fn = this.sayHello; this.elm.addEventListener(‘click’, fn); You are passing a function as the event handler, but have not ensured that when fn is called that this will be set to your desired value. The easiest way … Read more

Can I pre-declare variables for destructuring assignment of objects? [duplicate]

When you are destructuring an Object, you need to use the same variable names as the keys in the object. Only then you will get one to one correspondence and the values will be destructured properly. and you need to wrap the entire assignment in parenthesis if you are not using declaration statement, otherwise the … Read more

Why is Number.MAX_SAFE_INTEGER 9,007,199,254,740,991 and not 9,007,199,254,740,992?

I would say its because while Math.pow(2, 53) is the largest directly representable integer, its unsafe in that its also the first value who’s representation is also an approximation of another value: 9007199254740992 == 9007199254740993 // true In contrast to Math.pow(2, 53) – 1: 9007199254740991 == 9007199254740993 // false

Is it safe to delete elements in a Set while iterating with for..of?

Yes, it is perfectly fine to add elements and remove elements to a set while iterating it. This use case was considered and is supported in JavaScript 2015 (ES6). It will leave it in a consistent state. Note this also applies to itearting with forEach. Intuitively: The set iteration algorithm basically looks something like this: … Read more

In Nest.js, how to get a service instance inside a decorator?

Late to the party, but since I had a similar problem (Use global nest module in decorator) and stumbled upon this question. import { Inject } from ‘@nestjs/common’; export function yourDecorator() { const injectYourService = Inject(YourServiceClass); return (target: any, propertyKey: string, propertyDescriptor: PropertyDescriptor) => { // this is equivalent to have a constructor like constructor(yourservice: … Read more

Why is arrow syntax preferred over function declaration for functional React components?

I would say that this is a bit opinionated choice really. There are at least several reasons why I (personally) see arrow function use for a purely functional component as pretty bad practice. Here are those: Syntax abuse. When we define function component we don’t need to pre-bind its context to a specific scope. The … Read more