JavaScript

JavaScript

Made by DeepSource

Should not use attrs in ember hooks JS-0777

Performance
Minor
ember

In 2.0.0, didReceiveAttrs and didUpdateAttrs hooks were introduced. These hooks are called whenever the references of arguments to a component change. These hooks receive arguments, however one should not use them as they can be very costly when you have a lot of components on the page.

Bad Practice

import Component from '@ember/component';

export default Component({
  init(...args) {
    this._super(...args);
    this.updated = false;
  },
  didReceiveAttrs(attrs) {
    const { newAttrs, oldAttrs } = attrs;
    if (newAttrs && oldAttrs && newAttrs.value !== oldAttrs.value) {
      this.set('updated', true);
    } else {
      this.set('updated', false);
    }
  }
});

Recommended

import Component from '@ember/component';

export default Component({
  init(...args) {
    this._super(...args);
    this._valueCache = this.value;
    this.updated = false;
  },
  didReceiveAttrs() {
    if (this._valueCache !== this.value) {
      this._valueCache = this.value;
      this.set('updated', true);
    } else {
      this.set('updated', false);
    }
  }
});