Build a Loan EMI Calculator in React and Deploy it on GitHub Pages

Learn how to build a Loan EMI Calculator with React to compute monthly EMIs, total repayment, and interest. Deploy your app online for free using GitHub Pages.

ReactJS

GitHub Pages

Build a Loan EMI Calculator in React and Deploy it on GitHub Pages

Managing loan payments can be a daunting task, but with a simple Loan EMI Calculator, you can quickly compute monthly payments and understand your financial commitments. In this article, we'll walk through building a Loan EMI Calculator using React.js and deploy it online for free using GitHub Pages. By the end, you'll have a functional app to calculate EMIs for Home Loans, Car Loans, and Personal Loans.


๐Ÿš€ What We'll Cover

  1. Setting up a React project.
  2. Implementing the EMI calculation formula.
  3. Building the UI for input and results.
  4. Deploying the app to GitHub Pages.

Video Tutorial if you don't like to read complete blog


๐Ÿ› ๏ธ Step 1: Set Up the React Project

Start by setting up a React app using Create React App.

pnpm create vite
// Follow instructions
cd into project folder
pnpm install
pnpm run dev

This creates the boilerplate for your React project and runs a development server.


๐Ÿงฎ Step 2: Understand the EMI Formula

The formula for calculating EMI (Equated Monthly Installment) is:

[ EMI = \frac{P \times R \times (1 + R)^N}{(1 + R)^N - 1} ]

Where:

  • (P) = Loan principal
  • (R) = Monthly interest rate (( \text{annual rate} / 12 / 100 ))
  • (N) = Loan tenure in months

We'll write a utility function in src/utils/calculateLoanDetails.js to compute this.


calculateLoanDetails.js

export function calculateLoanDetails(principal, annualRate, tenureYears) {
    const monthlyRate = annualRate / 12 / 100;
    const tenureMonths = tenureYears * 12;
    const emi =
        (principal * monthlyRate * Math.pow(1 + monthlyRate, tenureMonths)) /
        (Math.pow(1 + monthlyRate, tenureMonths) - 1);
    const totalAmount = emi * tenureMonths;
    const totalInterest = totalAmount - principal;

    return {
        monthlyEMI: emi.toFixed(2),
        totalPrincipal: principal.toFixed(2),
        totalInterest: totalInterest.toFixed(2),
        totalAmount: totalAmount.toFixed(2),
    };
}

๐ŸŽจ Step 3: Build the UI

In the App.js file, build a form for user input and a results display section.

App.js

import React, { useState } from 'react';
import { calculateLoanDetails } from './utils/calculateLoanDetails';

function App() {
    const [principal, setPrincipal] = useState('');
    const [rate, setRate] = useState('');
    const [tenure, setTenure] = useState('');
    const [result, setResult] = useState(null);

    const handleCalculate = () => {
        const details = calculateLoanDetails(Number(principal), Number(rate), Number(tenure));
        setResult(details);
    };

    return (
        <div style={{ padding: '20px' }}>
            <h1>Loan EMI Calculator</h1>
            <div>
                <label>Principal Amount: </label>
                <input
                    type="number"
                    value={principal}
                    onChange={(e) => setPrincipal(e.target.value)}
                />
            </div>
            <div>
                <label>Annual Interest Rate (%): </label>
                <input
                    type="number"
                    value={rate}
                    onChange={(e) => setRate(e.target.value)}
                />
            </div>
            <div>
                <label>Loan Tenure (Years): </label>
                <input
                    type="number"
                    value={tenure}
                    onChange={(e) => setTenure(e.target.value)}
                />
            </div>
            <button onClick={handleCalculate}>Calculate EMI</button>

            {result && (
                <div>
                    <h2>Results:</h2>
                    <p>Monthly EMI: โ‚น{result.monthlyEMI}</p>
                    <p>Total Interest: โ‚น{result.totalInterest}</p>
                    <p>Total Amount Payable: โ‚น{result.totalAmount}</p>
                </div>
            )}
        </div>
    );
}

export default App;

๐ŸŒ Step 4: Deploy to GitHub Pages

Install the GitHub Pages Package

Add the gh-pages package to your project:

npm install gh-pages --save-dev

Configure package.json

Add the following fields to your package.json:

"homepage": "https://<your-username>.github.io/loan-emi-calculator",
"scripts": {
    "predeploy": "npm run build",
    "deploy": "gh-pages -d build"
}

Replace <your-username> with your GitHub username.


Deploy the App

  1. Initialize a Git repository:

    git init
    git remote add origin https://github.com/<your-username>/loan-emi-calculator.git
    
  2. Deploy to GitHub Pages:

    npm run deploy
    

Your app will be live at https://<your-username>.github.io/loan-emi-calculator. ๐ŸŽ‰


๐ŸŽฏ Whatโ€™s Next?

  • Add support for different currencies.
  • Include an amortization schedule for detailed monthly breakdowns.

Congratulations! You've built and deployed a Loan EMI Calculator using React and GitHub Pages. Share your app with others and help them make informed financial decisions! ๐Ÿš€

For more tutorials like this, subscribe to my YouTube channel and follow me on GitHub.

Happy coding! ๐Ÿ˜Š


Get latest updates

I post blogs and videos on different topics on software
development. Subscribe newsletter to get notified.


You May Also Like

Build a MERN Stack File Upload App with Progress Bar and Metadata Storage

Build a MERN Stack File Upload App with Progress Bar and Metadata Storage

Learn how to create a MERN stack file upload app with real-time progress tracking and metadata storage in MongoDB.

Express.js Crash Course: Build a RESTful API with Middleware

Express.js Crash Course: Build a RESTful API with Middleware

Learn to build a RESTful API using Express.js, covering middleware, routing, and CRUD operations in just 30 minutes.

Can Next.js Replace Your Backend? Pros, Cons, and Real-World Use Cases

Can Next.js Replace Your Backend? Pros, Cons, and Real-World Use Cases

Explore whether Next.js can fully replace your backend server. Learn about the advantages, limitations, and use cases for using Next.js as a full-stack solution for modern web development projects.