Loading please wait...

vtucircle » BCSL657B Program 3

BCSL657B Program 3

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

  1. Navigate to the src folder in the file explorer on the left-hand side of VSCode.
  2. Open the App.js file (which contains the default template code).
  3. Replace the content of App.js with the code provided for the Counter App. Here’s the code to replace inside App.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

  1. 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:

BCSL657B Program 3 Output 1

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

Error

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.

  1. In the terminal, navigate to your project folder (if not already there):
cd counter-app
  1. Install web-vitals by running the following command:
npm install web-vitals
  1. 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.

  1. 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();
  1. Save the file, and the application should compile without the error. You can now continue developing your app.
guest
1 Comment
Inline Feedbacks
View all comments
bhavana
bhavana
09-06-2025 7:15 PM

hello

1
0
Would love your thoughts, please comment.x
()
x