Skip to content

Instantly share code, notes, and snippets.

@stephencweiss
Last active December 10, 2018 15:02
Show Gist options
  • Save stephencweiss/844be18f4da56f985f534298d5e1fc45 to your computer and use it in GitHub Desktop.
Save stephencweiss/844be18f4da56f985f534298d5e1fc45 to your computer and use it in GitHub Desktop.
Binding methods in React supplies the appropriate context so that a function can be passed around.
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
}
this.searchDB = this.searchDB.bind(this);
this.handleClick = this.handleClick.bind(this); // This was the missing critical line
}
handleClick (type, param) {
if (type === "Search") {
this.searchDB(param);
}
else console.log('No type managed --> ', type);
}
searchDB (query) {
console.log(`The search query is --> `, query);
}
render () {
...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment