Dynamic key in immutability update helper for array

You could use ES6 computed property names, which would look like this with your index variable: updateValue(index, e) { var items = React.addons.update(this.state.items, { [index]: { amount: {$set: e.target.value} } }) this.setState({items}) } According to this ES6 compatability table, React’s JSX transpiler supports them when its Harmony transforms are enabled, or you could use Babel … Read more

Template literals syntax is not working in IE11

If you look at the ECMAScript 6 compatibility table, you’ll see that template literals are not supported by IE11. The “use strict”; statement doesn’t really change anything, because before it is determined whether a code is in strict mode, it has to be parsed first, but it can’t be parsed, because you’re using syntax that the … Read more

What is a good way to automatically bind JS class methods?

Use fat arrow function in ES6 (generally called as arrow function) anotherOne = ()=> { … } Call like this onClick={this.anotherOne}; no need to bind in constuctor From the ECMA spec Any reference to arguments, super, this, or new.target within an ArrowFunction must resolve to a binding in a lexically enclosing environment. Typically this will … Read more

array.push is not a function – when working with reduce [duplicate]

The return value of Array#push is the new length of the array after the push. This means that in the second iteration acc is a number, which doesn’t have the push method. The fix is simple – separate the push and return statements: const secondArray = [1, 2, 3].reduce((acc, item) => { acc.push(1); return acc; … Read more

Property change subscription with Aurelia

In some plugins I’ve been using DI to get the ObserverLocator instance from the container: import {inject} from ‘aurelia-dependency-injection’; // or from ‘aurelia-framework’ import {ObserverLocator} from ‘aurelia-binding’; // or from ‘aurelia-framework’ @inject(ObserverLocator) export class Foo { constructor(observerLocator) { this.observerLocator = observerLocator; } … } You can then do something like this: var subscription = this.observerLocator … Read more