3. Create a Counter Application using React that demonstrates state management with the useState hook. Display the current value of the counter prominently on the screen. Add buttons to increase and decrease the counter value. Ensure the counter updates dynamically when the buttons are clicked. Use the useState hook to manage the counter’s state within the component. Prevent the counter from going below a specified minimum value (e.g., 0). Add a “Reset” button to set the counter back to its initial value. Include functionality to specify a custom increment or decrement step value.
Step 1: Create a new React app
First, you need to create a new React app using below command. Open your terminal and run:
npx create-react-app counter-app
This will set up a new React project in a folder called counter-app
. After the installation is complete, navigate to the project directory:
cd counter-app
Step 2: Modify the App.js
File
- Navigate to the
src
folder in the file explorer on the left-hand side of VSCode. - Open the
App.js
file (which contains the default template code). - Replace the content of
App.js
with the code provided for the Counter App. Here’s the code to replace insideApp.js
:
import React, { useState } from 'react';
import './App.css';
function App() {
const [counter, setCounter] = useState(0);
const [step, setStep] = useState(1);
const minValue = 0;
const handleIncrement = () => {
setCounter(prevCounter => prevCounter + step);
};
const handleDecrement = () => {
if (counter - step >= minValue) {
setCounter(prevCounter => prevCounter - step);
}
};
const handleReset = () => {
setCounter(0);
};
const handleStepChange = (event) => {
setStep(Number(event.target.value));
};
return (
<div style={{ textAlign: 'center', marginTop: '50px' }}>
<h1>Counter Application</h1>
<div style={{ fontSize: '48px', margin: '20px' }}>
<span>{counter}</span>
</div>
<div>
<button onClick={handleIncrement}>Increase by {step}</button>
<button onClick={handleDecrement}>Decrease by {step}</button>
<button onClick={handleReset}>Reset</button>
</div>
<div style={{ marginTop: '20px' }}>
<label>
Set Increment/Decrement Step:
<input
type="number"
value={step}
onChange={handleStepChange}
min="1"
style={{ marginLeft: '10px' }}
/>
</label>
</div>
</div>
);
}
export default App;
Step 3: Modify the App.css
(Optional)
You can adjust the styling if desired. For example, you can modify App.css
to ensure the buttons look good:
.App {
text-align: center;
}
button {
margin: 10px;
padding: 10px;
font-size: 16px;
cursor: pointer;
}
input {
padding: 5px;
font-size: 16px;
}
You can also remove any default styling from the App.css
file that is not needed for this project.
Step 4: Start the Development Server
- In the terminal inside VSCode, run the following command to start the React development
npm start
- This will open your browser and navigate to
http://localhost:3000/
. You should see your Counter Application up and running.
OUTPUT:

“Fixing the Module not found: Error: Can't resolve 'web-vitals'
Error in React”

The error you’re seeing occurs because the web-vitals
package, which is used for performance monitoring in a React app, is not installed by default in the project or has been removed. Since web-vitals
is an optional package, you can safely resolve this issue by either installing the package or removing the code that imports it.
Option 1: Install the web-vitals
package
If you want to keep the performance monitoring functionality and resolve the error, simply install the web-vitals
package.
- In the terminal, navigate to your project folder (if not already there):
cd counter-app
- Install
web-vitals
by running the following command:
npm install web-vitals
- After installation is complete, restart the development server:
npm start
This should resolve the error, and your application should compile correctly.
Option 2: Remove the Web Vitals Code (If Not Needed)
If you don’t need performance monitoring and want to get rid of the error, you can safely remove the import and usage of web-vitals
from your code.
- Open
src/reportWebVitals.js
and remove its contents or just comment out the code:
// import { reportWebVitals } from './reportWebVitals';
// You can safely remove the call to reportWebVitals or leave it commented out
// reportWebVitals();
- Save the file, and the application should compile without the error. You can now continue developing your app.
hello