react state counter example

Mastering React: Incremental State Counter Example

By Rachel Walker
AD

In the realm of React development, state management is a predominant concern. One of the simplest yet/+vital elements of a React application's state is a counter. It tracks a singular value that typically increments or decrements, representing a count of some kind. In this article, we will delve into a comprehensive example of a React state counter, exploring its creation, functionality, and nuances.

Before we embark on our journey, ensure you have a basic understanding of React. If not, you might want to brush up on hooks, components, and state management in React.

Setting Up a Basic React Counter

To initiate our counter, we'll commence by setting up a new React application and creating a functional component. This component will eventually house our counter state and logic.

AD
⚛️ Every React developer starts with useState()… but not everyone understands it properly. If you're learning React, mastering useState is the first step to building dynamic and interactive applications. In this guide you'll learn: ✅ What useState is ✅ How it works ✅ Counter example ✅ Boolean, String & Array state ✅ Best practices ✅ Quick cheat sheet 💡 Once you understand useState, React becomes much easier to learn. 📌 Save this post for future reference. 📤 Share it with your coding frien... Best Practice, Cheat Sheets, First Step, Coding
⚛️ Every React developer starts with useState()… but not everyone understands it properly. If you're learning React, mastering useState is the first step to building dynamic and interactive applications. In this guide you'll learn: ✅ What useState is ✅ How it works ✅ Counter example ✅ Boolean, String & Array state ✅ Best practices ✅ Quick cheat sheet 💡 Once you understand useState, React becomes much easier to learn. 📌 Save this post for future reference. 📤 Share it with your coding frien... Best Practice, Cheat Sheets, First Step, Coding
React Context State
React Context State
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

For simplicity, let's create a basic counter that increments by 1 each time a button is clicked. Here's how you might proceed:

Initializing State and Counter Logic

Inside the functional component, we'll use the 'useState' hook to establish our counter state. This state will hold our count and serve as our primary focus for updates.

React projet setup
React projet setup
React JS Cheat Sheet
React JS Cheat Sheet
React Hooks: useState(using the state hook) | HackerNoon
React Hooks: useState(using the state hook) | HackerNoon

Your initial implementation could look something like this:

```jsx import React, { useState } from 'react'; function Counter() { const [count, setCount] = useState(0); } ```

In the following lines, we will expand on this basic setup, delving into how to increment the count and display it within the component.

AD

Incrementing the Counter

The 'setCount' function provided by 'useState' will allow us to update our count. Let's make a button that increments our counter by 1 whenever it's clicked:

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
React Cheat Sheet, React Coding, React Programming, Free Programming Books, React Code, Computer Science Programming, Css Tutorial, Coding Lessons, Learn Web Design
React Cheat Sheet, React Coding, React Programming, Free Programming Books, React Code, Computer Science Programming, Css Tutorial, Coding Lessons, Learn Web Design
the react component diagram is shown in this image
the react component diagram is shown in this image
a poster with different types of people and words on it that say react in short
a poster with different types of people and words on it that say react in short
State Management in React Query (2025 Guide for MERN Developers)
State Management in React Query (2025 Guide for MERN Developers)
an image of a web page with the text what is use state in react?
an image of a web page with the text what is use state in react?

The updated component might look like this:

```jsx function Counter() { const [count, setCount] = useState(0); const increment = () => { setCount(count + 1); }; } ```

Now, the next natural step is to display the count in a formatted manner within our component. Let's move to that in our subsequent section.

AD

Dynamic Counter Display

Up to this point, our counter is functional but invisible. Now, it's time to display our count within our component in a dynamic manner. We can leverage React's 'render' functionality to attain this:

an image of a web page with the words react basics written in different font and numbers
an image of a web page with the words react basics written in different font and numbers
React Fundamental
React Fundamental
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 diagram shows how to use react 18 and react 8 in different ways, including an email
the diagram shows how to use react 18 and react 8 in different ways, including an email
React State Management in 2026 — Redux vs Zustand vs Jotai vs TanStack Query
React State Management in 2026 — Redux vs Zustand vs Jotai vs TanStack Query
3 Important Things to Know About State in React
3 Important Things to Know About State in React
⚛️ 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
10 REACT PROJECTS EVERY FRONTEND DEVELOPER SHOULD BUILD
10 REACT PROJECTS EVERY FRONTEND DEVELOPER SHOULD BUILD
State in React Component - Tutorialswebsite
State in React Component - Tutorialswebsite
an info poster showing the different types of people in each country, and what they are doing
an info poster showing the different types of people in each country, and what they are doing
an image of a computer screen with the text react basics on it and icons below
an image of a computer screen with the text react basics on it and icons below
HugeDomains.com
HugeDomains.com
the react form status page for react formstatus, which includes text and symbols
the react form status page for react formstatus, which includes text and symbols

Displaying the Count

In 'React', you can display dynamic values directly within your JSX return statement. Here's how to display our count:

```jsx function Counter() { const [count, setCount] = useState(0); const increment = () => { setCount(count + 1); }; return (
Count: {count}
); } ```

The above code now shows the current count and a clickable "Increment" button. Clicking the button updates the count and reflects the new value in real-time, demonstrating how state updates work in React.

AD

Decrementing the Counter

As a full-featured React counter, it's beneficial to include a decrement feature alongside the increment. Here's how to add a decrement function and button:

The updated component might look like this:

```jsx function Counter() { const [count, setCount] = useState(0); const increment = () => { setCount(count + 1); }; const decrement = () => { setCount(count - 1); }; return (
Count: {count}
); } ```

With this setup, our counter now performs increment and decrement operations, reflecting real-time updates in state.

Exploring a state counter in React is just the starting point of understanding state management in React. Further exploration into 'useReducer' and Redux will undoubtedly benefit your understanding and application of state management in larger and more complex React applications.

AD