react counter component example

React Counter Component: Step-by-Step Example

By Laura White
AD

In the realm of front-end development, React, a JavaScript library for building user interfaces, has become a staple due to its component-based architecture. Among the most fundamental and commonly used components in React is the counter. Let's dive into an engaging, SEO-optimized example of a React counter component, exploring its creation, functionality, and use cases.

The React counter component, at its core, is a reusable piece of code that manages state in a declarative way. It incrementally counts, often used to display the number of times a user has performed an action or to track progress.

Creating a Basic Counter Component

The first step in creating a counter component involves setting up your React environment and establishing a new component file, say, 'Counter.js'.

AD
Components example in reacts and nextjs 14
Components example in reacts and nextjs 14
a blue background with the words react usestate hook on it and an image of a computer screen
a blue background with the words react usestate hook on it and an image of a computer screen
React projet setup
React projet setup

You'll start by importing the necessary React components and setting up the initial state for your counter.

Setting Up the Initial State

In React, state is managed using the 'useState' hook. For a counter, the initial state will be a count of 0. Here's how you'd set it up:

an anime character sitting on top of a chair with cats around her legs and the caption reads react server component
an anime character sitting on top of a chair with cats around her legs and the caption reads react server component
25 Best React UI Component Libraries for 2025: The Complete Developer’s Guide
25 Best React UI Component Libraries for 2025: The Complete Developer’s Guide
React JS Cheat Sheet
React JS Cheat Sheet

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

Defining the Counterplets

The counter needs two functions: one to increment the count and another to decrement it. These are our counterplets.

AD

The setCount() function, given by the 'useState' hook, updates the state of our component. Here's how you'd create these:

How to Create an Editable Table Component in React
How to Create an Editable Table Component in React
The Ultimate React Hooks Cheat Sheet: 7 Hooks in One Visual Guide
The Ultimate React Hooks Cheat Sheet: 7 Hooks in One Visual Guide
the react component diagram is shown in this image
the react component diagram is shown in this image
an image of a computer screen with the words basic react component anatomy
an image of a computer screen with the words basic react component anatomy
Input Character Counter JS - Code Snippet
Input Character Counter JS - Code Snippet
Handling errors globally using higher order component in react
Handling errors globally using higher order component in react

const incrementCount = () => {
  setCount(count + 1);
}

const decrementCount = () => {
  if (count > 0) {
    setCount(count - 1);
  }
}

Developing a More Feature-Rich Counter

While our basic counter is functional, let's enhance it with some additional features, such as Reset, Increment by a custom number, and Displaying the count difference with a target number.

Resetting the Counter

To reset the counter, we simply need to reset the count back to 0.

AD

Add a new function resetCount() that calls setCount(0).

React Tabs Component | Reusable UI | Interactive Web Development
React Tabs Component | Reusable UI | Interactive Web Development
React Component Lifecycle — Complete Visual Guide (Class + Hooks)
React Component Lifecycle — Complete Visual Guide (Class + Hooks)
ReactJS Pagination Tutorial: Building from Scratch [VIDEO]
ReactJS Pagination Tutorial: Building from Scratch [VIDEO]
10 Beautiful React + Tailwind CSS Components You Can Copy-Paste
10 Beautiful React + Tailwind CSS Components You Can Copy-Paste
25 REACT HOOKS EVERY DEVELOPER SHOULD KNOW
25 REACT HOOKS EVERY DEVELOPER SHOULD KNOW
the logo for 10 super useful react components, librarians and tools
the logo for 10 super useful react components, librarians and tools
React Modal with react context api
React Modal with react context api
React Context State
React Context State
React Fundamental
React Fundamental
the number seven is displayed in green, black and white colors with numbers on it
the number seven is displayed in green, black and white colors with numbers on it
4 React Design Patterns You Should Know
4 React Design Patterns You Should Know
React.js Cheat Sheet 2026 ⚛️ | Complete React Hooks, Components & JSX Quick Reference
React.js Cheat Sheet 2026 ⚛️ | Complete React Hooks, Components & JSX Quick Reference
React Cheat Sheet You Must Save | Beginner Coding Guide
React Cheat Sheet You Must Save | Beginner Coding Guide
⚛️ React Cheat Sheet Every Frontend Developer Should Save! 🚀 Learning React? Don't try to memorize everything. Instead, understand the core concepts and keep this cheat sheet handy while building projects. 📌 This cheat sheet covers: ✅ Components ✅ JSX ✅ Props ✅ State ✅ useState ✅ useEffect ✅ useRef ✅ useContext ✅ Conditional Rendering ✅ Lists & Keys ✅ Event Handling ✅ Forms ✅ React Router ✅ Useful Snippets Whether you're building your first React app or preparing for interviews, this guid... Frontend Developer, React App, Cheat Sheets, Router, How To Memorize Things
⚛️ React Cheat Sheet Every Frontend Developer Should Save! 🚀 Learning React? Don't try to memorize everything. Instead, understand the core concepts and keep this cheat sheet handy while building projects. 📌 This cheat sheet covers: ✅ Components ✅ JSX ✅ Props ✅ State ✅ useState ✅ useEffect ✅ useRef ✅ useContext ✅ Conditional Rendering ✅ Lists & Keys ✅ Event Handling ✅ Forms ✅ React Router ✅ Useful Snippets Whether you're building your first React app or preparing for interviews, this guid... Frontend Developer, React App, Cheat Sheets, Router, How To Memorize Things

Incrementing by a Custom Number

A useful feature for counters is being able to increment by a specified amount, rather than always incrementing by 1. This can be achieved by adding a state for the increment step and adjusting our 'incrementCount' function to use it.

Displaying Count Difference with a Target

For motivating users, showing the difference between the current count and a target can be a powerful feature. To achieve this, we'd need to add a 'target' state and calculate the difference in our render method.

AD

The final step is to use your counter component in your React application. You can import it and use it like any other React component. With your counter component ready, you can now build various interactive features that require tracking, like 'Likes', 'Shares', 'Visits', ' recovered Projects', etc.

Conclusion: React Counter Component in Action

Our journey with the React counter component has taken us from a simple state management exercise to a versatile feature that can be used in various aspects of your front-end. Embracing this component's full potential can greatly enhance the interactivity of your React applications.

So, why not start with adding a 'Counter' to your 'Likes' feature, or monitor 'Visits' in your application's dashboard? With React and its powerful components like the counter at your disposal, the sky's the limit.

AD