Skip to content

Instantly share code, notes, and snippets.

@jethrolarson
Created December 14, 2016 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jethrolarson/1c0927276651eecb08115a4c90fcae57 to your computer and use it in GitHub Desktop.
Save jethrolarson/1c0927276651eecb08115a4c90fcae57 to your computer and use it in GitHub Desktop.
Bind all solutions
import React, {createClass} from 'react';
class MediaLibrary = createClass({
foo() {
this.setState({foo: !this.state.foo})
},
render() {
return (<Bar onFoo={this.foo} />);
}
}
import React, {Component} from 'react';
import bindAll from 'util/class';
@bindAll
class MediaLibrary extends Component {
foo() {
this.setState({foo: !this.state.foo})
}
render() {
return (<Bar onFoo={this.foo} />);
}
}
import React, {Component} from 'react';
class MediaLibrary extends Component {
foo() {
this.setState({foo: !this.state.foo})
}
render() {
return (<Bar onFoo={() => this.foo()} />);
}
}
import React, {Component} from 'react';
class MediaLibrary extends Component {
foo() {
this.setState({foo: !this.state.foo})
}
render() {
return (<Bar onFoo={this.foo.bind(this)} />);
}
}
import React, {Component} from 'react';
class MediaLibrary extends Component {
constructor(){
super()
this.foo = this.foo.bind(this);
}
foo() {
this.setState({foo: !this.state.foo})
}
render() {
return (<Bar onFoo={this.foo} />);
}
}
import React from 'react';
import Component from 'util/reactExt';
class MediaLibrary extends Component {
foo() {
this.setState({foo: !this.state.foo})
}
render() {
return (<Bar onFoo={this.foo} />);
}
}
@jethrolarson
Copy link
Author

Notes:

  • local binds create new function instances on every invocation of render
  • decorator is a pretty raw TC39 proposal and requires an experimental babel plugin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment