Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • React Tutorial
  • React Exercise
  • React Basic Concepts
  • React Components
  • React Props
  • React Hooks
  • React Router
  • React Advanced
  • React Examples
  • React Interview Questions
  • React Projects
  • Next.js Tutorial
  • React Bootstrap
  • React Material UI
  • React Ant Design
  • React Desktop
  • React Rebass
  • React Blueprint
  • JavaScript
  • Web Technology
Open In App
Next Article:
How to create a simple counter Using ReactJS?
Next article icon

How to create a simple counter Using ReactJS?

Last Updated : 09 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

React counter app is a program that allows users to interact with a numerical counter value. It demonstrates basic state management and user interaction within a user interface.

Prerequisites:

  • NPM & Node.js
  • React JS
  • React useState hook

Approach:

Building a simple counter application is a great way to practice your React skills. Now, to create a simple counter app in React:

  • Create a useState variable named count and setCount() to update the state.
  • Create buttons named increment and decrement that modify the count state +1 and -1 respectively.
  • Link the buttons with setCount using event handlers.

We'll be creating a simple application where we have 2 buttons one to increment and one to decrement.

Steps to Create React Application:

Step 1: Create a React application using the following command:

npx create-react-app counter

Step 2: After creating your project folder i.e. counter, move to it using the following command:

cd counter

Project Structure:

Project Structure

Example: This example demonstrate a simple counter app using useState hook, and the count increase and descrease on click.

JavaScript
// Filename - App.js  import React, { useState } from "react";  // Importing app.css is css file to add styling import "./App.css";  const App = () => {     //  Counter is a state initialized to 0     const [counter, setCounter] = useState(0);      // Function is called everytime increment button is clicked     const handleClick1 = () => {         // Counter state is incremented         setCounter(counter + 1);     };      // Function is called everytime decrement button is clicked     const handleClick2 = () => {         // Counter state is decremented         setCounter(counter - 1);     };      return (         <div             style={{                 display: "flex",                 flexDirection: "column",                 alignItems: "center",                 justifyContent: "center",                 fontSize: "300%",                 position: "absolute",                 width: "100%",                 height: "100%",                 top: "-15%",             }}         >             Counter App             <div                 style={{                     fontSize: "120%",                     position: "relative",                     top: "10vh",                 }}             >                 {counter}             </div>             <div className="buttons">                 <button                     style={{                         fontSize: "60%",                         position: "relative",                         top: "20vh",                         marginRight: "5px",                         backgroundColor: "green",                         borderRadius: "8%",                         color: "white",                     }}                     onClick={handleClick1}                 >                     Increment                 </button>                 <button                     style={{                         fontSize: "60%",                         position: "relative",                         top: "20vh",                         marginLeft: "5px",                         backgroundColor: "red",                         borderRadius: "8%",                         color: "white",                     }}                     onClick={handleClick2}                 >                     Decrement                 </button>             </div>         </div>     ); };  export default App; 

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: This output will be visible on the http://localhost:3000/ on the browser window.


Next Article
How to create a simple counter Using ReactJS?

P

praveshsshetty
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • CSS
  • ReactJS

Similar Reads

    How to Create a Simple Counter App using Next.js ?
    This article explains the process of creating a simple counter application using Next.js. We will see how to build a functional counter with increment and decre­ment buttons. In a typical scenario, imagine the need to create a user-friendly counte­r application. The application would require buttons
    3 min read
    How to Create Countdown Timer using React Native ?
    Our main objective focuses on constructing a straightforward and use­r-friendly countdown timer that impeccably showcase­s the remaining time le­ft in terms of years, days, hours, minutes, and seconds until a specific date.To set up your de­velopment environment, begin by installing Node.js. Next, u
    4 min read
    How to Create a Basic Notes App using ReactJS ?
    Creating a basic notes app using React JS is a better way to learn how to manage state, handle user input, and render components dynamically. In this article, we are going to learn how to create a basic notes app using React JS. A notes app is a digital application that allows users to create, manag
    4 min read
    How to Create Countdown Timer in React JS
    React timers are very common UI components that are widely used in various applications and websites to visually display the remaining time for a specific activity or event. React timers are mostly used to highlight the commencement or conclusion of events or offers on commercial websites.This tutor
    8 min read
    How to Create Scroll Indicator using ReactJS ?
    Scroll Indicator in React JS refers to a representation of the length of the page visited or present on the screen. It shows the amount the pages that have been scrolled. PrerequisiteNode.js & npm React JSstyled-componentsReact JS useState() hooks.Approach to create Scroll IndicatorTo create a S
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences