React JS
June 3, 2026 5 min read

Introduction to React JS: Building User Interfaces with Efficiency

HS
HEMANT SINGH
Technical Writer & Educator

What is React JS?

React JS is a JavaScript library for building user interfaces.

It was developed by Meta (Facebook).

React allows us to break a large user interface into small, reusable components.

A component is usually a JavaScript function that returns UI using JSX.

Installing React JS

Modern React projects are commonly created using tools such as Vite.

Create a React project using:

npm create vite@latest my-app

Then install the dependencies:

cd my-app
npm install

Start the development server:

npm run dev

Creating a React Component

A React component can be created using a JavaScript function.

function Header() {
return (
<header>
Header of the web page
</header>
);
}

export default Header;

A component returns JSX that describes what should appear on the screen.

Importing and Exporting Components

A component can be exported from one file and imported into another.

Export

function Register() {
return (
<h1>Register Form</h1>
);
}

export default Register;

Import

import Register from "./Register";

The imported component can then be used:

<Register />

JSX

JSX is a syntax extension for JavaScript that allows us to write HTML-like syntax inside JavaScript.

Example:

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

Important JSX Rules

  1. Return a single parent element.
  2. Close all HTML tags properly.
  3. Use className instead of class.
  4. Use curly brackets {} to write JavaScript expressions.

Example:

function App() {

let name = "Aman";

return (
<div>
<h1>Hello {name}</h1>
</div>
);
}

React Fragment

A Fragment allows us to group multiple elements without adding an extra HTML element.

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

<>...</> is called a React Fragment.

JavaScript in JSX

Use {} to insert JavaScript expressions into JSX.

let name = "Aman";
let age = 25;

function App() {
return (
<h1>
Hello {name}, you are {age} years old.
</h1>
);
}

You can also use expressions:

<p>Total: {10 + 20}</p>

Output:

Total: 30

Styling in React

Inline styles are written using an object.

<h1 style={{ backgroundColor: "yellow", color: "blue" }}>
Header
</h1>

The first {} tells JSX that JavaScript is being used.

The second {} represents the JavaScript object containing the styles.

Props

Props are used to pass data from a parent component to a child component.

function Person({ name, age }) {
return (
<p>
Hello {name}, you are {age} years old.
</p>
);
}

Pass props:

<Person name="Aman" age={25} />

Output:

Hello Aman, you are 25 years old.

Props are read-only.

Conditional Rendering

Conditional rendering means displaying different UI based on a condition.

let isLoggedIn = true;

function App() {
return (
<div>
{isLoggedIn ? (
<h1>Welcome</h1>
) : (
<h1>Please Login</h1>
)}
</div>
);
}

Rendering Lists

The map() method is commonly used to display a list.

let fruits = ["Apple", "Mango", "Banana"];

function App() {
return (
<ul>
{fruits.map((fruit) => (
<li key={fruit}>{fruit}</li>
))}
</ul>
);
}

The key helps React identify list items.

Event Handling

React can respond to events such as clicks.

function App() {

function handleClick() {
alert("Button clicked");
}

return (
<button onClick={handleClick}>
Click Me
</button>
);
}

Common events include:

onClick
onChange
onSubmit
onMouseOver

Hooks

Hooks are functions that allow React components to use features such as state and side effects.

Common hooks include:

  1. useState()
  2. useEffect()
  3. useReducer()
  4. useContext()
  5. useCallback()
  6. useMemo()
  7. useRef()

useState()

useState() is used to create and update state.

import { useState } from "react";

function App() {

let [user, setUser] = useState("");

return (
<div>
<p>Your name is {user}</p>

<button onClick={() => setUser("Aman")}>
Update User
</button>
</div>
);
}

useState() returns two values:

user → Current state value
setUser → Function used to update the state

When the state changes, React re-renders the component.

useEffect()

useEffect() is used to perform side effects in a component.

Examples of side effects include:

  1. Fetching data
  2. Timers
  3. Subscribing to events
  4. Updating external systems

Import:

import { useEffect } from "react";

Run After Every Render

useEffect(() => {
console.log("Component rendered");
});

Without a dependency array, the effect runs after every completed render.

Run Once

An empty dependency array makes the effect run after the initial render.

useEffect(() => {
console.log("Component loaded");
}, []);

Run When a Value Changes

useEffect(() => {
console.log("User changed");
}, [user]);

The effect runs when user changes.

useReducer()

useReducer() is useful for managing more complex state logic.

It works with:

State + Action + Reducer

Basic example:

import { useReducer } from "react";

function reducer(count, action) {

switch (action.type) {

case "increment":
return count + 1;

case "decrement":
return count - 1;

default:
return count;
}
}

function App() {

let [count, dispatch] = useReducer(reducer, 0);

return (
<div>

<p>Count: {count}</p>

<button onClick={() => dispatch({ type: "increment" })}>
+
</button>

<button onClick={() => dispatch({ type: "decrement" })}>
-
</button>

</div>
);
}

Here:

count → Current state
dispatch → Sends an action
reducer → Decides how the state changes

useContext()

useContext() allows components to access shared data without passing props through every level.

Example:

import { createContext, useContext } from "react";

const UserContext = createContext();

function Profile() {

let user = useContext(UserContext);

return <h1>Hello {user}</h1>;
}

It is useful for shared data such as:

  1. User information
  2. Theme
  3. Language
  4. Application settings

useCallback()

useCallback() returns a memoized function.

It can help when passing functions to optimized child components.

let handleClick = useCallback(() => {
console.log("Clicked");
}, []);

The function is recreated when its dependencies change.

useMemo()

useMemo() returns a memoized value.

It can be useful when a calculation is expensive.

let total = useMemo(() => {
return price * quantity;
}, [price, quantity]);

The calculation is performed again when price or quantity changes.

useRef()

useRef() is commonly used to store a value that should persist between renders or to access a DOM element directly.

Example:

import { useRef } from "react";

function App() {

let inputRef = useRef();

function focusInput() {
inputRef.current.focus();
}

return (
<div>

<input ref={inputRef} />

<button onClick={focusInput}>
Focus Input
</button>

</div>
);
}