create counter app in react

"Build Counter App with React: Step-by-Step Guide 2023"

By Brian Garcia
AD

In the ever-evolving landscape of mobile app development, React emerges as a beloved framework for building intuitive and dynamic user interfaces. This article delves into the process of creating a counter app using React, a straightforward yet powerful project that unveils the framework's core concepts. Let's embark on this learning journey and explore how to craft a functional and engaging counter app.

Before diving into the code, ensure you have Node.js and npm installed. If you haven't, download and install them from the official website (https://nodejs.org/). Once set up, create a new React project using Create React App (https://create-react-app.dev/):

Setting Up the React Project

Let's initialize a new React project called "counter-app." Open your terminal or command line, navigate to your preferred working directory, and run the following command:

AD
Counter App With React JS | React App For Beginners
Counter App With React JS | React App For Beginners
Building an Animated Counter with React and CSS
Building an Animated Counter with React and CSS
My Row Counter App
My Row Counter App

npx create-react-app counter-app

Once created, navigate into the new project directory with the command: cd counter-app

React projet setup
React projet setup
Create New React App
Create New React App
10 REACT PROJECTS EVERY FRONTEND DEVELOPER SHOULD BUILD
10 REACT PROJECTS EVERY FRONTEND DEVELOPER SHOULD BUILD

Project Directory Structure

Your new project should have the following initial directory structure:

AD
counter-app/
  ├─ node_modules/
  ├─ public/
  ├─ src/
  │   ├─ App.css
  │   ├─ App.js
  │   ├─ index.css
  │   └─ index.js
  ├─ .gitignore
  ├─ package.json
  └─ README.md

React in 30 Seconds
React in 30 Seconds
5 Ways to Make Your React Apps Faster in 2025 ⚡
5 Ways to Make Your React Apps Faster in 2025 ⚡
20 REACT HOOKS EVERY DEVELOPER SHOULD KNOW
20 REACT HOOKS EVERY DEVELOPER SHOULD KNOW
Build Your First React App (2 Hours)
Build Your First React App (2 Hours)
React Performance Optimization — 10 Techniques to Make Your App Blazing Fast
React Performance Optimization — 10 Techniques to Make Your App Blazing Fast
react server components are connected to each other with the same symbol in front of them
react server components are connected to each other with the same symbol in front of them

The folders and files in this structure will be integral to your React project. Now, let's start building our counter app.

Creating the Counter Component

Navigate to the "src" directory and create a new file named "Counter.js." This file will house the main logic of our counter app. Inside this file, import React and define the Counter component:

AD

import React, { useState } from "react";

4 Custom Hooks to Boost Your React App
4 Custom Hooks to Boost Your React App
Calculator App using React JS with Source Code
Calculator App using React JS with Source Code
A Complete React Todo App using React, Redux, Framer Motion
A Complete React Todo App using React, Redux, Framer Motion
Building a Timer App with React Native: A Beginner’s Tutorial
Building a Timer App with React Native: A Beginner’s Tutorial
the steps to creating a react app with vite
the steps to creating a react app with vite
I created the exact same app in React and Vue. Here are the differences.
I created the exact same app in React and Vue. Here are the differences.
How to make your React Native app respond gracefully when the keyboard pops up
How to make your React Native app respond gracefully when the keyboard pops up
Build a to-do list app in React with hooks
Build a to-do list app in React with hooks
React for Beginners - Build a Todo-List App
React for Beginners - Build a Todo-List App
Create theme with zustand in react dark theme
Create theme with zustand in react dark theme
5 Ways to animate a ReactJs app in 2019.
5 Ways to animate a ReactJs app in 2019.
How to Create an Editable Table Component in React
How to Create an Editable Table Component in React
React | Day 1
React | Day 1
𝐑𝐞𝐚𝐜𝐭 𝐒𝐢𝐧𝐠𝐥𝐞 𝐏𝐚𝐠𝐞 𝐀𝐩𝐩𝐥𝐢𝐜𝐚𝐭𝐢𝐨𝐧 (𝐒𝐏𝐀)
𝐑𝐞𝐚𝐜𝐭 𝐒𝐢𝐧𝐠𝐥𝐞 𝐏𝐚𝐠𝐞 𝐀𝐩𝐩𝐥𝐢𝐜𝐚𝐭𝐢𝐨𝐧 (𝐒𝐏𝐀)

const Counter = () => {

const [count, setCount] = useState(0);

AD

Here, we're using the useState hook, a state variable that allows the component to keep track of its internal state and update rerenders accordingly.

Building the Component's UI

The UI for our counter app consists of a display for the current count and two buttons for incrementing and decrementing the value.

Displaying the Current Count

First, add the following code to display the current count:

<div>

<h1>Counter: {count}</h1>

<button onClick={() => setCount(count + 1)}>Increment</button>

<button onClick={() => setCount(Math.max(count - 1, 0))}>Decrement</button>

</div>

The first button increments the count, while the second decrements it. To prevent negative values, we use Math.max to ensure the count never goes below 0.

Styling the Counter Component

Now, let's style our counter app for a polished look. Create a new file, "Counter.css," in the "src" directory and add the following CSS:

.counter {
  text-align: center;
}
.counter h1 {
  font-size: 3rem;
}
.counter button {
  margin: 0.5rem;
  padding: 0.25rem 1rem;
  font-size: 1rem;
}

Import the CSS file at the top of the "Counter.js" file and apply the styles:

import "./Counter.css";

<div className="counter">

...

</div>

Now, let's integrate this Counter component into our main App component.

Integrating the Counter Component

Importing and Using the Counter Component

Navigate back to "App.js" and import the Counter component at the top:

import Counter from "./Counter";

Then, add the Counter component to the App's return statement:

<div className="App">

<Counter />

</div>

With that, we've successfully created a simple yet functional counter app using React!

To explore more and build upon this project, consider adding features like a reset button, persistent state using localStorage, or even creating multiple counters. As you grow with React, this counter app serves as an excellent foundation for future projects.

Happy coding, and may your counter app reach new heights!

AD