react usestate counter example

React UseState Counter Example

By Ashley Miller
AD

The hook in React is widely used for managing component-specific state. A common example of its application is a simple counter. In this article, we delve into the world of the hook, explaining its purpose and providing a step-by-step guide on how to create a counter application using it.

Before we dive into the counter app, it's crucial to understand what the hook is and when it's used. Introduced in React 16.8, allows you to add React state to functional components. It's a way to "remember and keep track of something" in between renders, which distinguishes it from default props values.

Understanding useState

In essence, is a function that takes an initial state and returns an array with two elements:

AD
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
Building an Animated Counter with React and CSS
Building an Animated Counter with React and CSS
The Ultimate React Hooks Cheat Sheet: 7 Hooks in One Visual Guide
The Ultimate React Hooks Cheat Sheet: 7 Hooks in One Visual Guide
  • Current state (usually a variable that stores the state)
  • A function that updates the state (mostly a callback thatMutation the state)

The state hook becomes indispensable when you want to create local component states, without resorting to complex context APIs or reducers. It enables you to efficiently update component UI in response to user actions or data changes.

Initializing State with useState

You can initialize state with in a few lines of code. Here's how:

React projet setup
React projet setup
⚛️ 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 JS Cheat Sheet
React JS Cheat Sheet
import React, { useState } from 'react';

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

  return (
    

You clicked {count} times

); }

In the above example, count is the state variable, and setCount is the function to update it.

Updating State with useState

Updating state using involves calling the mutation function: ```jsx setCount(count + 1) ``` This will trigger a re-render to update the component's UI.

AD

Creating a Counter App with useState

Equipped with the knowledge of how works, let's proceed to create a simple counter application.

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 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
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 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 Form Handling 2026 — React Hook Form vs Server Actions vs TanStack Form
React Form Handling 2026 — React Hook Form vs Server Actions vs TanStack Form
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?

Our counter app will include two buttons - 'Increment' and 'Decrement'. Incrementing will increase the count by one, while decrementing will decrease it by one, unless it hits zero. The count should always be displayed on the screen.

Initiating the Counter State

First, initialize the state with , setting the count to zero:

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

Implementing the Counter Buttons

Now, implement two buttons, one for increment and one for decrement:

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
10 REACT PROJECTS EVERY FRONTEND DEVELOPER SHOULD BUILD
10 REACT PROJECTS EVERY FRONTEND DEVELOPER SHOULD BUILD
⚛️ 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
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
React Fundamental
React Fundamental
the react building ui is designed to look like a puzzle
the react building ui is designed to look like a puzzle
React Context State
React Context State
React Hooks Explained in the Simplest Way 🪝
React Hooks Explained in the Simplest Way 🪝
How to Create an Editable Table Component in React
How to Create an Editable Table Component in React
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 Tabs Component | Reusable UI | Interactive Web Development
React Tabs Component | Reusable UI | Interactive Web Development
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
React Beginner Projects 2026
React Beginner Projects 2026
```jsx ```

Notice that decrement button's logic checks to ensure the count does not go below zero.

The hook simplifies state management in functional components. Once you're comfortable with it, you're equipped to build more complex React applications.

AD

Remember, the key to using is to update the state immutably. Avoid directly modifying the state. Instead, create a new state object or copy an existing one with the changes. Happy coding!

AD