,

Creating a Simple Age Calculator App in React: A Beginner’s Project

Posted by






Build Age Calculator App in React: Project for beginner

Build Age Calculator App in React: Project for beginner

React is a popular JavaScript library for building user interfaces. It is known for its flexibility, simplicity, and performance. In this article, we will walk through the steps to build an age calculator app using React. This project is suitable for beginners who are just getting started with React.

Prerequisites

Before we begin, make sure you have Node.js and npm installed on your machine. You can download and install them from https://nodejs.org/.

Setting up the Project

First, let’s create a new React project using Create React App. Open your terminal and run the following command:

npx create-react-app age-calculator

This command will create a new folder called age-calculator with all the necessary files and dependencies for a React project.

Once the project is created, navigate to the project folder using the following command:

cd age-calculator

Building the Age Calculator

Open the project in your favorite code editor and navigate to the src folder. You will see several files, including App.js, App.css, App.test.js, and index.js. Let’s focus on the App.js file for now.

Replace the contents of the App.js file with the following code:


import React, { useState } from 'react';

function App() {
const [birthYear, setBirthYear] = useState('');
const [age, setAge] = useState(0);

const calculateAge = () => {
const currentYear = new Date().getFullYear();
const calculatedAge = currentYear - birthYear;
setAge(calculatedAge);
}

return (

Age Calculator

setBirthYear(e.target.value)}
/>

Your age is: {age}

);
}

export default App;

Save the file and run the following command in your terminal to start the development server:

npm start

Open your web browser and navigate to http://localhost:3000. You should see the age calculator app running in your browser. Try entering your birth year and clicking the “Calculate Age” button to see the app in action.

Conclusion

Congratulations! You have successfully built an age calculator app using React. This project serves as a great introduction to React for beginners. Feel free to explore and customize the app further by adding features such as input validation and styling. Happy coding!


0 0 votes
Article Rating
7 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Cand Dev
11 months ago

I noticed a problem with the calculation of months and days in the code. I have already addressed this issue and made the necessary updates in the code on my GitHub repository. You can check the description of this video for the link to the updated code. 🚀

Nasssty
11 months ago

hey, thanks for tutorials <3 i would like to see how you would add alert(''enter date'') upon clicking the disabled button(when date is not defined)… Thanks !

Amrit Shrestha
11 months ago

can you make NextJS project a little bit more complex than Todo app.

RianY2K
11 months ago

thank you for tutorial

angga erlangga
11 months ago

Mantap

anonymous_dev
11 months ago

This is not the kind of project that was expected from you, expecting something good…

kisnak nugroho
11 months ago

nice projek mas🔥