Events


<button onClick = { function() { alert('click'); } >  


It is good convention to have event handlers defined as methods in the component class.

method_HandleEvent = event => { 
   this.setState({ string : this.state.sText === 'a' ? 'b' : 'a' })
}


class MyButton extends React.Component { 
   render() {
      return (
         <button onClick = { anyGlobalFunction } >
         </button>
      );
   }
}

Or it can be defined on the class component as an instance property

class MyButton extends React.Component { 
   handleOnClick = () => { anyGlobalFunction };

   render() {
      return (
         <button onClick = { this.handleOnClick } >
         </button>
      );
   }
}

This is equivalent to

<MyButton onClick = { () => { anyGlobalFunction }  } > 


Passing Parameters to Event Handlers


<MyButton onClick = { () => { this.handleClick(id) } > 

equivalent to

<MyButton onClick = { this.handleClick.bind(this,id}  } > 


React.synthetic event

React implements a synthetic events system
Synthetic events in a cross browser wrapper round the browsers native event.





© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext