react counter app example

React Counter App: Build a Simple Example in 5 Minutes

By Michael White
AD

Diving into the realm of React, a popular JavaScript library for building user interfaces, one of the first and simplest components you'll create is often a counter app. This handy little tool not only serves as a excellent beginner project, but also demonstrates fundamental concepts like state and props. Let's explore how to create a simple React counter app step by step.

Before we get started, ensure you have Node.js and npm installed on your machine. Then, install Create React App globally to simplify the process of setting up a new React project: `npm install -g create-react-app`.

Setting Up the React Counter App

The first step is creating a new React app. In your terminal, run `create-react-app counter-app`. Then, navigate to the new directory: `cd counter-app`.

AD
React projet setup
React projet setup
the counter clock is displayed in black and red
the counter clock is displayed in black and red
Live Step Counter
Live Step Counter

Now, let's create our counter component by making a new file called `Counter.js` in the `src` directory.

Creating the Counter State

In React, data that changes over time is stored in a component's state. Let's initialize our counter state to 0 in the `Counter` component's constructor.

My Row Counter App
My Row Counter App
React Native Wallet Animation
React Native Wallet Animation
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

Here's how you might do that:

import React, { Component } from 'react';

class Counter extends Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
  }
}

Increasing and Decreasing the Counter

Next, we need methods to increment and decrement the counter. We'll update the state by using the `setState` function after each click.

AD

Here's how you can do that:

Building a Timer App with React Native: A Beginner’s Tutorial
Building a Timer App with React Native: A Beginner’s Tutorial
the user's guide to implement notifications in reactt, including icons and text
the user's guide to implement notifications in reactt, including icons and text
Build Your First React App (2 Hours)
Build Your First React App (2 Hours)
Build ReacJS App
Build ReacJS App
Counter-App
Counter-App
4 Custom Hooks to Boost Your React App
4 Custom Hooks to Boost Your React App
incrementCount = () => {
  this.setState({ count: this.state.count + 1 });
}

decrementCount = () => {
  this.setState({ count: this.state.count - 1 });
}

Building the Counter User Interface

Now that we've set up the state and methods, it's time to create the UI. In the `Counter` component's `render` method, let's create buttons to increment and decrement the count, and a display to show the current count.

Here's how you might create the UI:

AD
render() {
  return (
    

{this.state.count}

); }

Displaying the Counter

Finally, we need to include our `Counter` component in `App.js`. Replace the content of `App.js` with:

Create New React App
Create New React App
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
Click and Drag Counter
Click and Drag Counter
12+ Fantastic React Calculator Examples - OnAirCode
12+ Fantastic React Calculator Examples - OnAirCode
308c7f39c311b2cd3396cc.png | Are.na
308c7f39c311b2cd3396cc.png | Are.na
Turn Steps Into a Journey — Fitness App Concept
Turn Steps Into a Journey — Fitness App Concept
exhausted
exhausted
Swipe to delete with React native
Swipe to delete with React native
an iphone app showing the progress of project progress from 6 to 5 minutes ago, with people on it
an iphone app showing the progress of project progress from 6 to 5 minutes ago, with people on it
Counter Widget — (6) Streak Effect Animations • Count Anything // Wins Losses Counter for Twitch
Counter Widget — (6) Streak Effect Animations • Count Anything // Wins Losses Counter for Twitch
10 REACT PROJECTS EVERY FRONTEND DEVELOPER SHOULD BUILD
10 REACT PROJECTS EVERY FRONTEND DEVELOPER SHOULD BUILD
5 Ways to animate a ReactJs app in 2019.
5 Ways to animate a ReactJs app in 2019.
5 Ways to Make Your React Apps Faster in 2025 ⚡
5 Ways to Make Your React Apps Faster in 2025 ⚡
import React from 'react';
import './App.css';
import Counter from './Counter';

function App() {
  return (
    
); } export default App;

That's it! Now, when you start your app with `npm start`, you should see a simple counter app with a current count of 0. Each time you click "+" or "-", the count changes accordingly.

Building a React counter app is a fantastic way to understand how state works in React. From here, you can build more complex components and apps. Why not try adding a reset button, or making the counter display in red when it's negative? The possibilities are endless!

AD
AD