Skip to content

Instantly share code, notes, and snippets.

@danethurber
Created April 26, 2017 17:42
Show Gist options
  • Save danethurber/446f110985875d0e8479faed52f3d99b to your computer and use it in GitHub Desktop.
Save danethurber/446f110985875d0e8479faed52f3d99b to your computer and use it in GitHub Desktop.
React Binding
class TestComponent extends Component() {
handleClick(evt) {
console.log(evt)
}
render() {
return <button onClick={e => this.handleClick(e)}>Click Me</button>
}
}
import { autobind } from 'core-decorators'
@autobind
class TestComponent extends Component() {
handleClick() {
console.log(evt)
}
render() {
return <button onClick={this.handleClick}>Click Me</button>
}
}
class TestComponent extends Component() {
handleClick(evt) {
console.log(evt)
}
render() {
return <button onClick={this.handleClick.bind(this)}>Click Me</button>
}
}
class TestComponent extends Component() {
handleClick = (evt) => {
console.log(evt)
}
render() {
return <button onClick={this.handleClick}>Click Me</button>
}
}
class TestComponent extends Component() {
constructor(props) {
super(props)
this.handleClick = ::this.handleClick
}
handleClick(evt) {
console.log(evt)
}
render() {
return <button onClick={this.handleClick}>Click Me</button>
}
}
class TestComponent extends Component() {
constructor(props) {
super(props)
this.handleClick = this.handleClick.bind(this)
}
handleClick(evt) {
console.log(evt)
}
render() {
return <button onClick={this.handleClick}>Click Me</button>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment