-1

I have this code

class BottomPanel extends React.Component<Props, {}> {
  constructor(props) {
    super(props);
    this.dropDownUpdate = this.dropDownUpdate.bind(this);
  }

  dropDownUpdate = e => this.setState({ dropDownValue: e.currentTarget.textContent });

I was wondering if I can refactor

this.dropDownUpdate = this.dropDownUpdate.bind(this);

to

this.dropDownUpdate = this.dropDownUpdate;

if so, why or why not? I just saw it somewhere being done this way

this.dropDownUpdate = this.dropDownUpdate;

I'm not sure if I can apply it to my code.

user1176111
  • 111
  • 1
  • 11

3 Answers3

2

One thing to be aware of, is that when you write:

class BottomPanel extends React.Component<Props, {}> {
  dropDownUpdate = e => this.setState({ dropDownValue: e.currentTarget.textContent });
}

This (currently) is not valid ES6 syntax. You can't assign a class variable with an equals sign that way. The reason it works is because of the transform-class-properties babel plugin, more on that below.

See this question and answer for more details:

ES6 class variable alternatives

Technically, the right way to declare a class variable is with the this keyword like:

class BottomPanel extends React.Component<Props, {}> {
  constructor(props) {
      this.dropDownUpdate = this.dropDownUpdate.bind(this)
  }

   dropDownUpdate(e) {
       this.setState({ dropDownValue: e.currentTarget.textContent });
   }
}

For why you can't just do:

class BottomPanel extends React.Component<Props, {}> {
  constructor(props) {
      this.dropDownUpdate =e => this.setState({ dropDownValue: e.currentTarget.textContent });
  }
}

see this post: Can I use arrow function in constructor of a react component?

Now the thing is - I assume that you are using Create React App - which comes with the transform-class-properties babel plugin - which is why that first declaration works.

Take a look at this question as to why you would want to to the this.dropDownUpdate = this.dropDownUpdate.bind(this); trick. It's not necessary - but you run into a debugger issue (the browser not being aware of the transform-class-properties babel transform).

Chrome, Firefox debuggers not displaying the correct value for 'this' in a react app

Otherwise you can just clear your class methods like:

class BottomPanel extends React.Component<Props, {}> {
  dropDownUpdate = e => this.setState({ dropDownValue: e.currentTarget.textContent });
}
dwjohnston
  • 11,163
  • 32
  • 99
  • 194
1

Since dropDownUpdate is an Arrow function, It should work as expected without

this.dropDownUpdate = this.dropDownUpdate; Or

this.dropDownUpdate = this.dropDownUpdate.bind(this);

In Arrow functions, value of this is lexically bound, unlike regular functions where the value of this changes based on the context in which the function is called.

phpcoderx
  • 570
  • 5
  • 16
0

Just define your function as below.

dropDownUpdate = (event) => { .... }

It wont need to bind in constructor

Dhara Charola
  • 332
  • 2
  • 12
  • This only works if you are using the `transfrom-class-properties` babel plugin (which comes with create-react-app) – dwjohnston Apr 19 '19 at 06:31