📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this chapter, we will learn how to handle events in React applications.
Handling events basics
- React events are named using camelCase, rather than lowercase — onClick instead of onclick.
- We write React event handlers inside curly braces.
- In the case of React(/JSX), rather than passing a string, we pass a function as the event handler. onClick={buttonClicked} instead of onclick=”buttonClicked()”.
<button onclick=”buttonClicked()”>
Click the Button!
</button>
<button onClick={buttonClicked}>
Click the Button!
</button>
- Another difference is that in order to prevent default behavior to React, we cannot return false. We have to call preventDefault explicitly. For example, the HTML:
// HTML
<button onclick="console.log('Button clicked.'); return false">
Click Me
</button>
// React
function handleClick(e) {
e.preventDefault();
console.log('Button clicked.');
}
return (
<button onClick={handleClick}>
Click me
</a>
);
Handling onClick Event in the Functional Component Example
import React from 'react'
function FunctionClick() {
function clickHandler() {
console.log('Button clicked')
}
return (
<div>
<button onClick={clickHandler}>Click</button>
</div>
)
}
export default FunctionClick;
Handling onClick Event in the Class Component Example
import React, { Component } from 'react'
class ClassClick extends Component {
clickHandler() {
console.log('Clicked the button')
}
render() {
return (
<div>
<button onClick={this.clickHandler}>Click Me</button>
</div>
)
}
}
export default ClassClick
Binding Event
import React, { Component } from 'react'
class EventBind extends Component {
constructor() {
super()
this.state = {
message: 'Hello'
}
}
clickHandler() {
console.log(this)
this.setState({message: 'Goodbye'})
}
render() {
return (
<div>
<div>{this.state.message}</div>
<button onClick={this.clickHandler}>Click</button>
</div>
)
}
}
export default EventBind
TypeError: Cannot read property 'setState' of undefined
1. Dynamic Binding :
import React, { Component } from 'react'
class EventBind extends Component {
constructor() {
super()
this.state = {
message: 'Hello'
}
}
clickHandler() {
console.log(this)
this.setState({message: 'Goodbye'})
}
render() {
return (
<div>
<div>{this.state.message}</div>
<button onClick={this.clickHandler.bind(this)}>Click</button>
</div>
)
}
}
export default EventBind
2. Using Arrow Function
import React, { Component } from 'react'
class EventBind extends Component {
constructor() {
super()
this.state = {
message: 'Hello'
}
}
clickHandler = () => {
this.setState({message:'Goodbye'})
}
render() {
return (
<div>
<div>{this.state.message}</div>
<button onClick={this.clickHandler}>Click</button>
</div>
)
}
}
export default EventBind
3. Constructor Binding :
import React, { Component } from 'react'
class EventBind extends Component {
constructor() {
super()
this.state = {
message: 'Hello'
}
this.clickHandler = this.clickHandler.bind(this)
}
clickHandler() {
console.log(this)
this.setState({message: 'Goodbye'})
}
render() {
return (
<div>
<div>{this.state.message}</div>
<button onClick={this.clickHandler}>Click</button>
</div>
)
}
}
export default EventBind
Passing Arguments
import React, { Component } from 'react'
class EventBind extends Component {
constructor() {
super()
this.state = {
message: 'Hello'
}
}
clickHandler = (param) => {
this.setState({message:param})
}
render() {
return (
<div>
<div>{this.state.message}</div>
<button onClick={() => this.clickHandler("Goodbye")}>Click</button>
</div>
)
}
}
export default EventBind
What's Next?
❮ Previous Chapter Next Chapter ❯
Comments
Post a Comment
Leave Comment