Binding event handlers

Слайд 2

THIS IN EVENT HANDLERS

When you define a component using an ES6 class, a

THIS IN EVENT HANDLERS When you define a component using an ES6
common pattern is for an event handler to be a method on the class.
If you forget to bind this. clickHandler and pass it to onClick, this will be undefined when the function is actually called.

export default class BindingDemonstration extends Component {
  constructor(props) {
  super(props)
this.state = {
   counter: 0
  }   
  }
clickHandler() {
  this.setState(prevState => ({counter: prevState.counter + 1}))
  }
  render() {
  return (
 


 
Counter: {this.state.counter}
 
 
 

  )
  }
}

Don’t forget to bind event handler!

Слайд 3

BINDING IN RENDER

We can bind in render method.
The problem with this syntax

BINDING IN RENDER We can bind in render method. The problem with
is that a different callback is created each time the BindingDemonstration renders. In most cases, this is fine.
However, if this callback is passed as a prop to lower components, those components might do an extra re-rendering.

export default class BindingDemonstration extends Component {
  constructor(props) {
  super(props)
this.state = {
   counter: 0
  }
  }
clickHandler() {
  this.setState(prevState => ({counter: prevState.counter + 1}))
  }
  render() {
  return (
 


 
Counter: {this.state.counter}
 
     
 

  )
  }
}

Слайд 4

ARROW FUNCTIOB IN RENDER

We can create an arrow function in render method.
The

ARROW FUNCTIOB IN RENDER We can create an arrow function in render
problem with this syntax is the same as with binding in render.

export default class BindingDemonstration extends Component {
  constructor(props) {
  super(props)
this.state = {
   counter: 0
  }
  }
clickHandler() {
  this.setState(prevState => ({counter: prevState.counter + 1}))
  }
  render() {
  return (
 


 
Counter: {this.state.counter}
 
     
 

  )
  }
}

Слайд 5

BINDING IN CONSTRUCTOR

Binding event handler in constructor is considered to be a

BINDING IN CONSTRUCTOR Binding event handler in constructor is considered to be
good practice.

export default class BindingDemonstration extends Component {
  constructor(props) {
  super(props)
this.state = {
   counter: 0
  }
  this.clickHandler = this.clickHandler.bind(this);
  }
clickHandler() {
  this.setState(prevState => ({counter: prevState.counter + 1}))
  }
  render() {
  return (
 


 
Counter: {this.state.counter}
 
 
 

  )
  }
}

Слайд 6

PUBLIC CLASS FIELDS SYNTAX

Defining event handler as a public class field is

PUBLIC CLASS FIELDS SYNTAX Defining event handler as a public class field
also considered to be a good practice.

export default class BindingDemonstration extends Component {
  constructor(props) {
  super(props)
this.state = {
   counter: 0
  }   
  }
clickHandler = () => {
  this.setState(prevState => ({counter: prevState.counter + 1}))
  }
  render() {
  return (
 


 
Counter: {this.state.counter}
 
 
 

  )
  }
}
Имя файла: Binding-event-handlers.pptx
Количество просмотров: 33
Количество скачиваний: 0