How to properly bind the current object context in ES6 using babelify

I'm trying to bind the current instance to a class method, note the ES6 syntax.

class SomeClass {

  search() => { ... }

}

Which is 100% legal code, however, babelify does not want to compile it

SyntaxError: /Users/vladmiller/Projects/test/test/client/test/app/pages/Search.react.js: Unexpected token (50:26) while parsing file: /Users/vladmiller/Projects/test/test/client/test/app/pages/Search.react.js\

Instead, now I need to bind the context in the class constructor

class SomeClass {
  constructor() {
    this.search = this.search.bind(this)
  }
  search() { ... }
}

It is quite annoying and boring.

UPD: Turns out this is an invalid ES6 syntax; therefore, the question is as follows. What is the best way to bind an instance context to a class method?

UPD2: By default, context should be added, but the problem with React is http://jsbin.com/citafaradu/2/edit?js,console,output

+1
source share
1

ES2015. :

class SomeClass {

  search() { /* ... */ }

}
+5

Source: https://habr.com/ru/post/1659242/


All Articles