Answer the question
In order to leave comments, you need to log in
How to call a class method in React without bind?
Hello. Not so long ago, an article slipped through Habré about increasing the performance of React, and there was such an anti-pattern
<SearchCollection onClick={() => { Alert('Click'); }}/>
<SearchCollection onClick={::this.clickHandler}/>
Answer the question
In order to leave comments, you need to log in
You can do this:
class MyComponent extends PureComponent {
clickHandler = () => {
// Так как это стрелочная функция - она будет забиндена при создании элемента (объекта)
}
render() {
return (
<SearchCollection onClick={this.clickHandler}/>
)
}
}
class MyComponent extends PureComponent {
constructor(props, context) {
super(props, context)
this.clickHandler = this.clickHandler.bind(this)
}
clickHandler() {
}
render() {
return (
<SearchCollection onClick={this.clickHandler}/>
)
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question