Hacker Timesnew | past | comments | ask | show | jobs | submitlogin

With the ES6 class syntax, is it possible to avoid the function.bind/anonymous function issue (and subsequent need for the whole IntermediateBinder thing) by redefining methods that will be passed into a subcomponent in the constructor to their bound equivalent? That is, in the constructor: `this.foo = this.foo.bind(this)` and in render: `<SubComponent foo={this.foo} />`?


What you described is the current replacement for auto-bind (that React.createClass used to do for you). The issue arises when you have not just one SubComponent, but n SubComponents that require the parent method bound to the subcomponent's index, e.g. you have a deleteItemAtIndex method, and you only want to expose {deleteItem: deleteItemAtIndex.bind(this, i)} to each child component. ES6 React classes don't have any special way of handling this.

To re-iterate the possible solutions I've considered:

- Don't pass in a bound method; give the component the unbound method and its index and the child component can call it with the index passed in itself. - Generalize this to a re-usable intermediary component that does for you (it does feel a bit dirty) - Write your own bind function that annotates the bound function with original function + params allowing you to do a "deep" equality check, and then use a variant of PureRenderMixin that does this "deep" equality check.

Honestly, all of the solutions feel a bit hacky, but I've gravitated towards the first and second options.


Ah, of course. Thank you for the clarification.


Try this: `class extends Component { foo = () => { console.log('this was bound at construction time'); }; render() { return <SubComponent foo={this.foo} />; } }` This only works if you have stage-1 class properties enabled though.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: