Using Events in React Js | YourSite

Using Events in React Js

React Js 2288 views

Question

1. Using Events in React Js

• Create a button that will increment the state of a variable count by 1.

• Use onClick event to call a function handleClick, and update the state of the count variable.

• Note: Make sure to use anonymous function using arrow function technique in onClick event to call a function handleClick.

Answer


import React, { Component } from 'react';


class App extends React.Component {
  // Initialize the state with a count variable set to 0
  state = {
    count: 0,
  };

  // This function will be called when the button is clicked
  handleClick = () => {
    // Increment the count by 1
    this.setState({ count: this.state.count + 1 });
  };

  // The render function returns the JSX that should be rendered
  render() {
    return (
      <div>
        {/* Display the count */}
        <p>Count: {this.state.count}</p>
        {/* Add a button with an onClick event that calls the handleClick function */}
        <button onClick={() => this.handleClick()}>Increment</button>
      </div>
    );
  }
}


export default App;

🚀 More Blogs You Might Like

Explore more articles and keep learning

What is Bounce Rate in SEO? Complete Guide for Beginners
search-engine-optimization
What is Bounce Rate in SEO? Complete Guide for Beginners

Learn what bounce rate is in SEO, how it is calculated, why it matters, common causes of high bounce rates, an...

👁 28 2026-05-24
Read More →
Comprehensive Interviewer Guide - Detailed Article
skill
Comprehensive Interviewer Guide - Detailed Article

Learn how to conduct effective interviews with this comprehensive interviewer guide. Explore hiring strategies...

👁 43 2026-05-22
Read More →
Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)
skill
Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)

Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)...

👁 38 2026-05-19
Read More →
How to Grow Your Business Mindset Step by Step
skill
How to Grow Your Business Mindset Step by Step

Learn how to develop and grow a successful business mindset step by step. Discover entrepreneurial thinking, p...

👁 56 2026-05-09
Read More →