Similar presentations:
Binding event handlers
1.
BINDING EVENTHANDLERS
by Nataliya Revutska
2.
THIS IN EVENT HANDLERSWhen you define a component using
export default class BindingDemonstration extends Component {
constructor(props) {
super(props)
an ES6 class, a common pattern is for an
event handler to be a method on the
this.state = {
counter: 0
}
class.
}
If you forget to bind this. clickHandler and
Don’t forget to bind
event handler!
clickHandler() {
this.setState(prevState => ({counter: prevState.counter + 1}))
}
pass it to onClick, this will be undefined
when the function is actually called.
render() {
return (
<div>
<div>Counter: {this.state.counter}</div>
<button onClick={this.clickHandler}>Click me</button>
</div>
)
}
}
3.
BINDING IN RENDERWe can bind in render method.
export default class BindingDemonstration extends Component {
constructor(props) {
super(props)
The problem with this syntax is that a
this.state = {
counter: 0
}
different callback is created each time the
BindingDemonstration renders. In most
}
cases, this is fine.
clickHandler() {
this.setState(prevState => ({counter: prevState.counter + 1}))
}
However, if this callback is passed as a
prop to lower components, those
render() {
return (
<div>
<div>Counter: {this.state.counter}</div>
<button onClick={this.clickHandler.bind(this)}>Click me</button>
</div>
)
}
components might do an extra rerendering.
}
4.
ARROW FUNCTIOB IN RENDERWe can create an arrow function in
export default class BindingDemonstration extends Component {
constructor(props) {
super(props)
render method.
this.state = {
counter: 0
}
The problem with this syntax is the
same as with binding in render.
}
clickHandler() {
this.setState(prevState => ({counter: prevState.counter + 1}))
}
render() {
return (
<div>
<div>Counter: {this.state.counter}</div>
<button onClick={() => this.clickHandler()}>Click me</button>
</div>
)
}
}
5.
BINDING IN CONSTRUCTORexport default class BindingDemonstration extends Component {
Binding event handler in
constructor(props) {
super(props)
constructor is considered to be a
this.state = {
counter: 0
}
good practice.
this.clickHandler = this.clickHandler.bind(this);
}
clickHandler() {
this.setState(prevState => ({counter: prevState.counter + 1}))
}
render() {
return (
<div>
<div>Counter: {this.state.counter}</div>
<button onClick={this.clickHandler}>Click me</button>
</div>
)
}
}
6.
PUBLIC CLASS FIELDS SYNTAXDefining event handler as a public
export default class BindingDemonstration extends Component {
constructor(props) {
super(props)
class field is also considered to be a
good practice.
this.state = {
counter: 0
}
}
clickHandler = () => {
this.setState(prevState => ({counter: prevState.counter + 1}))
}
render() {
return (
<div>
<div>Counter: {this.state.counter}</div>
<button onClick={this.clickHandler}>Click me</button>
</div>
)
}
}