Skip to main content

Python type() Function

When learning Python, you will work with different types of data, such as numbers, text, decimal values, and Boolean values. Python provides several built-in functions to help you work with these data types. One of the most useful functions is the type() function . The type() function tells you the data type of a value or variable. It helps you understand what kind of data Python is working with and can make debugging your programs easier. For example, consider these variables: age = 20 price = 99.50 name = "Rahul" is_student = True Although all four variables store data, their data types are different. Python allows us to check these types using type() . Let's understand how it works with simple examples. 1. What Is the type() Function in Python? The type() function is a built-in Python function that returns information about the type of an object. In beginner-level programming, you can use it to check whether a value is an integer, a floating-point number, a string,...

Introduction to React.js in the PERN Stack

Introduction

In the previous article, we learned how to build professional RESTful APIs using:

  • Express.js

  • PostgreSQL

  • Controllers

  • Routes

  • Middleware

Now it is time to move toward the frontend part of the PERN stack using React.js.

React is one of the most popular frontend libraries in the world and is used by companies like:

  • Facebook

  • Instagram

  • Netflix

  • Airbnb

  • WhatsApp Web

In this article, we will learn:

  • What React.js is

  • Why React is used

  • JSX

  • Components

  • Props

  • State

  • Event handling

  • React project structure

  • Connecting frontend basics


What is React.js?

React is a JavaScript library used to build modern and interactive user interfaces.

It was developed by:
Meta


Why React is Popular

React makes frontend development:

  • Faster

  • Easier

  • Reusable

  • Dynamic

Instead of reloading the entire page, React updates only the required parts of the UI.

This improves:

  • Performance

  • User experience

  • Speed


Traditional Websites vs React Applications

Traditional WebsiteReact Application
Full page reloadPartial UI updates
SlowerFaster
Less interactiveHighly interactive
Harder to manageComponent-based

What is a Component?

A component is a reusable part of the user interface.

Examples:

  • Navbar

  • Login form

  • Footer

  • Sidebar

  • Product card

React applications are built using multiple components.


Example of a Simple Component

function Welcome() {
    return <h1>Welcome to React</h1>;
}

export default Welcome;

Understanding the Code

Function Component

function Welcome()

creates a component.


Return Statement

return <h1>Welcome to React</h1>;

returns UI elements.


Export

export default Welcome;

allows the component to be used in other files.


What is JSX?

JSX stands for:

JavaScript XML

JSX allows us to write HTML-like code inside JavaScript.


Example of JSX

const element = <h1>Hello React</h1>;

Why JSX is Useful

JSX makes code:

  • Easier to read

  • Easier to write

  • More organized

Without JSX, React code becomes difficult to understand.


Rules of JSX

1. Return Only One Parent Element

Correct:

return (
    <div>
        <h1>Hello</h1>
        <p>Welcome</p>
    </div>
);

Wrong:

return (
    <h1>Hello</h1>
    <p>Welcome</p>
);

2. Use className Instead of class

Correct:

<div className="box"></div>

because class is a reserved keyword in JavaScript.


Creating Your First React Project

Open terminal and run:

npx create-react-app client

Move inside folder:

cd client

Start application:

npm start

Understanding React Project Structure

client
│
├── public
├── src
├── package.json
└── node_modules

Important Folders

FolderPurpose
publicStatic files
srcMain source code
node_modulesInstalled packages

Most Important File

src/App.js

This is the main React component.


Understanding App.js

Default React app:

function App() {
    return (
        <div>
            <h1>Hello React</h1>
        </div>
    );
}

export default App;

What are Props?

Props are used to pass data from one component to another.

Props stands for:

Properties

Example of Props

Child Component

function Student(props) {
    return <h1>{props.name}</h1>;
}

export default Student;

Parent Component

<Student name="Shivam" />

Output:

Shivam

Understanding Curly Braces {}

Inside JSX:

{}

is used to write JavaScript expressions.

Example:

<h1>{5 + 5}</h1>

Output:

10

What is State in React?

State stores dynamic data inside components.

Examples:

  • Counter values

  • Form data

  • User login status

  • API data


Using useState Hook

React provides:

useState()

for state management.


Example of useState

import { useState } from 'react';

function Counter() {

    const [count, setCount] = useState(0);

    return (
        <div>
            <h1>{count}</h1>

            <button onClick={() => setCount(count + 1)}>
                Increase
            </button>
        </div>
    );
}

export default Counter;

Understanding useState

const [count, setCount] = useState(0);
PartMeaning
countCurrent value
setCountFunction to update value
0Initial value

Event Handling in React

React handles events using:

  • onClick

  • onChange

  • onSubmit


Button Click Example

<button onClick={showMessage}>
    Click Me
</button>

Input Handling Example

<input
    type="text"
    onChange={(e) => console.log(e.target.value)}
/>

What is Virtual DOM?

React uses a concept called:

Virtual DOM

Instead of updating the real DOM directly, React:

  1. Creates virtual copy

  2. Finds changes

  3. Updates only required elements

This improves performance significantly.


Advantages of React

Reusable Components

Write once and use multiple times.


Faster Rendering

Virtual DOM improves speed.


Large Community Support

Huge ecosystem and tutorials available.


Easy Frontend Development

React simplifies complex UI creation.


Common Beginner Mistakes

Forgetting export default

Wrong:

function App() {}

Correct:

export default App;

Using class Instead of className

Wrong:

<div class="box"></div>

Correct:


Not Returning JSX Properly

Wrong:

function App() {
    <h1>Hello</h1>
}

Correct:

function App() {
    return <h1>Hello</h1>;
}

Real-World Uses of React

React is used in:

  • Social media platforms

  • Admin dashboards

  • E-commerce websites

  • Online learning platforms

  • SaaS applications

  • Chat applications


How React Fits in the PERN Stack

In the PERN stack:

  • PostgreSQL stores data

  • Express.js creates APIs

  • Node.js runs backend

  • React displays frontend UI

Complete Flow

React Frontend
       ↓
Express API
       ↓
Node.js Server
       ↓
PostgreSQL Database

Building Dynamic User Interfaces

React helps create:

  • Real-time updates

  • Interactive forms

  • Dynamic dashboards

  • Responsive applications

This is why React is extremely popular in modern web development.


Best Practices for React Beginners

Keep Components Small

Create separate components for:

  • Navbar

  • Footer

  • Forms

  • Cards


Use Meaningful Names

Good:

StudentCard

Bad:

Data1

Practice Component Reusability

Reusable code reduces development time.


Learn JavaScript Properly

Strong JavaScript knowledge improves React development skills.


Conclusion

You have now learned the fundamentals of React.js, the frontend part of the PERN stack.

You learned:

  • Components

  • JSX

  • Props

  • State

  • Event handling

  • Virtual DOM

  • React project structure

These concepts form the foundation of modern frontend development.

Now you are ready to connect React with your Express backend and build fully dynamic applications.

Comments

Popular posts from this blog

Understanding Comments in Java: A Beginner’s Guide

When learning Java programming, one of the first concepts you’ll come across is comments . Comments may not affect how a program runs, but they play a crucial role in making code more understandable, readable, and maintainable. Let’s dive deeper into what comments are, why they’re important, and the different types of comments in Java. What are Comments in Java? A comment in Java is a piece of text written within a program that is ignored by the compiler . In other words, comments are notes for humans, not instructions for machines. They help developers explain the purpose of code, describe logic, or temporarily disable certain lines of code without deleting them. Why are Comments Important? 📌 Readability – Comments make code easier to understand for you and others. 📌 Maintenance – When you revisit your code after months, comments remind you what you were doing. 📌 Collaboration – In team projects, comments explain code to fellow developers. 📌 Debugging – You c...

Java Program to Check Voting Eligibility

In this program, we’ll take the user’s age as input and check whether the person is eligible to vote or not. In India, the minimum age required to vote is 18 years . 💻 Program Code // Java Program to Check Voting Eligibility import java.util.Scanner; public class VotingEligibility { public static void main(String[] args) { // Create Scanner object to take input from user Scanner sc = new Scanner(System.in); // Ask user to enter age System.out.print("Enter your age: "); int age = sc.nextInt(); // Check eligibility if (age >= 18) { System.out.println("You are eligible to vote."); } else { System.out.println("You are not eligible to vote yet."); System.out.println("You can vote after " + (18 - age) + " year(s)."); } sc.close(); // Close the scanner } } 🧠 Explanation import java.util.Scanner; → Imp...

Java Program to Calculate Total Marks and Average of Five Subjects

💡 Program Explanation In this program: The user will input marks of five subjects . The program will calculate the total marks and average . It will then display both results clearly. 🧑‍💻 Java Code: // Java Program to Calculate Total Marks and Average of Five Subjects import java.util.Scanner; // Importing Scanner class for user input public class TotalAndAverage { public static void main(String[] args) { // Creating Scanner object to take input from user Scanner sc = new Scanner(System.in); // Declaring variables for five subjects double subject1, subject2, subject3, subject4, subject5; // Taking marks input for each subject System.out.print("Enter marks of Subject 1: "); subject1 = sc.nextDouble(); System.out.print("Enter marks of Subject 2: "); subject2 = sc.nextDouble(); System.out.print("Enter marks of Subject 3: "); subject3 = sc.nextDo...