react use counter

Mastering React: Increment with useCounter

By Laura Thomas
AD

Ever found yourself wishing you could easily keep track of state in your React application without having to lift it up to a parent component or pass it down as props? Well, wish no more! The React Hooks API introduced a game-changer: the `useState` Hook. Today, we're going to delve into one of its simplest yet most powerful use cases - creating a counter in React.

Before React Hooks, managing state in your functional components required you to convert them into classes or use a lib like Recompose. But not anymore! Now, you can enjoy the best of both worlds: functional components with state management capability. Let's get straight into it and create our first counter using `useState`.

Setting Up Our Counter

First, let's initialize our state variable using `useState`. This Hook allows us to add React state to functional components, giving us the ability to declare and update state without writing a class.

AD
React Hooks: useState(using the state hook) | HackerNoon
React Hooks: useState(using the state hook) | HackerNoon
The Ultimate React Hooks Cheat Sheet: 7 Hooks in One Visual Guide
The Ultimate React Hooks Cheat Sheet: 7 Hooks in One Visual Guide
React in 30 Seconds
React in 30 Seconds

In your component, import `useState` from React if you haven't already. Then, call this Hook inside the functional component. It returns an array with two values - the current state and a function to update it:

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

Initial State

We've initialized our state variable, `count`, with a value of 0. This means that when our component renders for the first time, the display value will be 0. But how do we update this state?

React projet setup
React projet setup
25 REACT HOOKS EVERY DEVELOPER SHOULD KNOW
25 REACT HOOKS EVERY DEVELOPER SHOULD KNOW
Trinket React Counter
Trinket React Counter

Updating Our Counter

The `useState` Hook returns a function named `setCount` which we can use to update our `count` state. We should also create two buttons, one for incrementing the counter and another for decrementing it.

Incrementing The Counter

To increment our counter, all we need to do is call `setCount` with the new value. In this case, we want to increase `count` by 1. Here's how we can implement this:

AD
```jsx ```

Decrementing The Counter

Similarly, to decrement the counter, we can pass `count - 1` as the new state value:

React Performance Optimization — 10 Techniques to Make Your App Blazing Fast
React Performance Optimization — 10 Techniques to Make Your App Blazing Fast
React
React
the counter clock is displayed in black and red
the counter clock is displayed in black and red
React Logo Png, React Logo Transparent, React Logo On Blue Background, React Logo, Official React Logo, React Native Logo On Blue Background, Best Practices For React Developers, React Developer Essentials, React Desktop Development
React Logo Png, React Logo Transparent, React Logo On Blue Background, React Logo, Official React Logo, React Native Logo On Blue Background, Best Practices For React Developers, React Developer Essentials, React Desktop Development
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
𝐑𝐞𝐚𝐜𝐭 𝐒𝐢𝐧𝐠𝐥𝐞 𝐏𝐚𝐠𝐞 𝐀𝐩𝐩𝐥𝐢𝐜𝐚𝐭𝐢𝐨𝐧 (𝐒𝐏𝐀)
𝐑𝐞𝐚𝐜𝐭 𝐒𝐢𝐧𝐠𝐥𝐞 𝐏𝐚𝐠𝐞 𝐀𝐩𝐩𝐥𝐢𝐜𝐚𝐭𝐢𝐨𝐧 (𝐒𝐏𝐀)
```jsx ```

The `onClick` event listener calls our `setCount` function every time the corresponding button is clicked, ensuring our state stays in sync with the display value. Isn't that neat?

Displaying Our Counter

Of course, it'd be pretty pointless if we couldn't see our counter in action! We can display the current count by simply including it in our JSX:

AD
```jsx

Count: {count}

an image of the react logo on a black and white background with text that reads react
an image of the react logo on a black and white background with text that reads react
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
20 REACT HOOKS EVERY DEVELOPER SHOULD KNOW
20 REACT HOOKS EVERY DEVELOPER SHOULD KNOW
When should you use React?
When should you use React?
Top 7 React Hooks Every Developer Should Know
Top 7 React Hooks Every Developer Should Know
Lessons Learned: Common React Code-Smells and How to Avoid Them | HackerNoon
Lessons Learned: Common React Code-Smells and How to Avoid Them | HackerNoon
Critical CVE-2025-55182 in React Enables Remote Code Execution
Critical CVE-2025-55182 in React Enables Remote Code Execution
React Context State
React Context State
GitHub - evareitzel/react-hooks-use-effect-cleanup
GitHub - evareitzel/react-hooks-use-effect-cleanup
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 Context API vs Redux: When and How to Use Each – Djamware
React Context API vs Redux: When and How to Use Each – Djamware
Can You Fetch Data with React Server Actions?
Can You Fetch Data with React Server Actions?
```

Complete Code

Here's what your complete `Counter` component might look like:

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

Count: {count}

); } ```

And there you have it - your very own reactive counter using `useState`! This example provides a solid foundation for building more stateful components and applications with React Hooks.

AD
AD