react counter app with input value for counter

React Counter App with Custom Input Value

By Rachel King
AD

In the dynamic world of web development, building interactive components is a crucial skill. One of the most fundamental and yet powerful interactive elements you can create is a counter app. This isn't just about displaying a static number; it's about engaging users and providing immediate feedback. In this guide, we'll demonstrate how to create a React counter app that allows users to input and update the counter's initial value.

React, a JavaScript library for building user interfaces, is perfect for this task. It allows us to create reusable components that manage their own state and can efficiently update and render to the DOM when their state changes. Let's dive into creating a counter app with an input value for the counter in React.

Setting Up the React Counter App

The first step is to set up a new React project. If you haven't already, install Create React App globally using npm (Node Package Manager):

AD
Counter-App
Counter-App
the counter clock is displayed in black and red
the counter clock is displayed in black and red
Bootstrap 5 Counter Up Animation with JavaScript
Bootstrap 5 Counter Up Animation with JavaScript

npx create-react-app counter-app

Then, navigate to your new project directory:

Click and Drag Counter
Click and Drag Counter
Calculator App using React JS with Source Code
Calculator App using React JS with Source Code
5 Ways to Make Your React Apps Faster in 2025 ⚡
5 Ways to Make Your React Apps Faster in 2025 ⚡

cd counter-app

Creating the Counter Component

We'll start by creating a new JavaScript file called Counter.js and defining our Counter component. This component will initially have a simple button that increments a counter value.

AD

import React, { useState } from 'react';

Live Step Counter
Live Step Counter
Building a Timer App with React Native: A Beginner’s Tutorial
Building a Timer App with React Native: A Beginner’s Tutorial
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
12+ Fantastic React Calculator Examples - OnAirCode
12+ Fantastic React Calculator Examples - OnAirCode
the dashboard shows that you can find what customers are using
the dashboard shows that you can find what customers are using
React projet setup
React projet setup

const Counter = () => {

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

AD

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

Build Your First React App (2 Hours)
Build Your First React App (2 Hours)
React Performance Optimization — 10 Techniques to Make Your App Blazing Fast
React Performance Optimization — 10 Techniques to Make Your App Blazing Fast
React Native Wallet Animation
React Native Wallet Animation
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
Building a Calculator for Android and iOS with React Native
Building a Calculator for Android and iOS with React Native
range_component
range_component
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
a calculator is displayed on an iphone's screen, displaying the time and date
a calculator is displayed on an iphone's screen, displaying the time and date
two different colored numbers on a black and red background with the time left to right
two different colored numbers on a black and red background with the time left to right
UI8 | The Ultimate Marketplace for Designers
UI8 | The Ultimate Marketplace for Designers
an app showing the number of people in korea
an app showing the number of people in korea
OBS Stream Counter Overlay | Score, Gacha Tracker
OBS Stream Counter Overlay | Score, Gacha Tracker
an app that shows the time and date for different things
an app that shows the time and date for different things
the top 10 new updates in react 19, which are available for all users to use
the top 10 new updates in react 19, which are available for all users to use

return (

<div>

AD

<p>You clicked {count} times</p>

<button onClick={incrementCount}>Click me</button>

</div>

);

export default Counter;

Adding Input for Counter Value

Next, we'll enhance our counter app to allow users to input the initial value. We'll use the useState and useEffect hooks to handle both the input value and the initial load of the component.

First, let's update our import statement to account for the new useEffect hook:

import React, { useState, useEffect } from 'react';

Now, update the Counter component as follows:

const Counter = () => {

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

const [inputValue, setInputValue] = useState('0');

useEffect(() => {

setCount(parseInt(inputValue, 10));

}, [inputValue]);

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

// ... (Rest of the component remains the same)

// Add this line just before the closing div tag:

<input type="number" value={inputValue} onChange={e => setInputValue(e.target.value)} />

Integrating the Counter Component

Now that we've created the Counter component, we need to integrate it into our application. Open the App.js file in your project's root directory and update it to include the Counter component:

import React from 'react';

import './App.css';

import Counter from './Counter';

function App() {

return (

<div className="App">

<h1>React Counter App</h1>

<Counter />

</div>

);

export default App;

With these steps, you've successfully created a React counter app that allows users to input an initial counter value. Users can then click the button to increment the counter starting from their input value.

Remember, practice makes perfect. Keep exploring more interactive components and state management techniques in React to elevate your web development skills. Until next time, happy coding!

AD