
Hi, welcome here in this tutorial we are going to develop a React-Hook-Expense-Tracker App and also use Tailwind CSS for styling, The prerequisites for this project are as follows:
- Node.js should be installed in your system.
- Create a brand new ReactJs project. Just copy the code below:
code to create React js project:
npx create-react-app my-app
after it run the below command:
cd my-app
after it run the below commands to install Tailwind css into your project:
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
after it run the below command to run your react app:
npm start
The brand-new React Js project should look like this:

Here is your code for the App.js file, copy the code below and paste it into your App.js file:
import logo from './logo.svg';
import './App.css';
import Budget from './components/Budget';
import Remaining from './components/Remaining';
import ExpenseList from './components/ExpenseList';
import AddExpenseForm from './components/AddExpenseForm';
import Spent from './components/Spent';
import { AppProvider } from './context/AppContext';
function App() {
return (
<>
>
);
}
export default App;
Here is your code for the index.css file, copy the code below and paste it into your index.css file:
@tailwind base;
@tailwind components;
@tailwind utilities;
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
Here is your code for tailwind.config.js copy the code below and paste it into tailwind.config.js.
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./src/**/*.{js,jsx,ts,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}
Make a new component folder and in the folder make the following new files and name then according to the instructions below.
Make a new file ‘AddExpenseForm.jsx’ and then Copy the code below and paste it into AddExpenseForm.jsx
import React, { useState, useContext } from 'react'
import { v4 as uuidv4 } from 'uuid';
import { AppContext } from '../context/AppContext';
const AddExpenseForm = () => {
const [name, setName] = useState('')
const [cost, setCost] = useState('')
const { dispatch } = useContext(AppContext)
const onSubmit = (e) => {
e.preventDefault();
const expense = {
id: uuidv4(),
name: name,
cost: parseInt(cost)
};
dispatch({
type: 'ADD_EXPENSE',
payload: expense,
})
setName('');
setCost('')
}
const btnDis = name== '' || cost == '' ? 'disabled' : '';
return (
<>
>
)
}
export default AddExpenseForm
Make a new file ‘Budget.jsx’ and then Copy the code below and paste it into Budget.jsx
import React, { useContext } from 'react'
import { AppContext } from '../context/AppContext'
const Budget = () => {
const { budget } = useContext(AppContext);
return (
Budget:
{budget} pkr
)
}
export default Budget
Make a new file ‘ExpenseItem.jsx’ and then Copy the code below and paste it into ExpenseItem.jsx
import React, { useContext } from 'react'
import { TiDelete } from 'react-icons/ti';
import { AppContext } from '../context/AppContext';
const ExpenseItem = (props) => {
const { dispatch } = useContext(AppContext)
const handleDeleteExpense = () => {
dispatch({
type: 'DELETE_EXPENSE',
payload: props.id
});
};
return (
{props.name}
{props.cost} pkr
)
}
export default ExpenseItem
Make a new file ‘ExpenseList.jsx’ and then Copy the code below and paste it into ExpenseList.jsx
import React, { useContext } from 'react'
import { AppContext } from '../context/AppContext';
import ExpenseItem from './ExpenseItem';
const ExpenseList = () => {
const {expenses} = useContext (AppContext);
return (
{expenses.map((expense) => (
))}
)
}
export default ExpenseList
Make a new file ‘Remaining.jsx’ and then Copy the code below and paste it into Remaining.jsx
import React, { useContext } from 'react';
import { AppContext } from '../context/AppContext';
const Remaining = () => {
const { expenses, budget } = useContext(AppContext);
const totalExpenses = expenses.reduce((total, item) => {
return (total = total + item.cost);
}, 0);
const alertType = totalExpenses > budget ? 'bg-red-100 border-red-400 text-red-700' : 'bg-green-100 border-green-400 text-green-700';
return (
Remaining:
{budget - totalExpenses} pkr
)
}
export default Remaining
Make a new file ‘Spent.jsx’ and then Copy the code below and paste it into Spent.jsx
import React, { useContext } from 'react'
import { AppContext } from '../context/AppContext'
const Spent = () => {
const { expenses } = useContext(AppContext)
const totalExpenses = expenses.reduce((total, item) => {
return (total += item.cost);
}, 0);
return (
Spent:
{totalExpenses} pkr
)
}
export default Spent
Make a context folder and then make a new file ‘AppContext.jsx’ and then Copy the code below and paste it into AppContext.jsx
import { useReducer, createContext } from 'react';
const AppReducer = (state, action) => {
switch (action.type) {
case 'ADD_EXPENSE':
return {
...state,
expenses: [...state.expenses, action.payload],
};
case 'DELETE_EXPENSE':
return {
...state,
expenses: state.expenses.filter(
(expense) => expense.id !== action.payload
),
};
default:
return state;
}
};
const initialState = {
budget: prompt("Please enter your budget: "),
// budget: 12000,
expenses: [
// { id: 12, name: 'shopping', cost: 40 },
// { id: 13, name: 'holiday', cost: 400 },
// { id: 14, name: 'car service', cost: 50 },
],
};
export const AppContext = createContext();
export const AppProvider = (props) => {
const [state, dispatch] = useReducer(AppReducer, initialState);
return (
{props.children}
);
};
After writing the code into the files (don’t forget to save it ,) you can run the command below:
npm start