React.js Tutorial: Understanding Components, Hooks, and State Management

React.js, commonly known as React, is a powerful JavaScript library for building user interfaces. Developed and maintained by Meta (formerly Facebook), React has revolutionized the way developers create web applications by enabling the development of reusable and efficient UI components. Since its release in 2013, React has become one of the most popular frontend libraries in the world, used by companies like Facebook, Instagram, Airbnb, Netflix, and many more.
This blog explores React.js in detail, covering its features, architecture, key concepts, advantages, and real-world applications. By the end, you’ll have a solid understanding of how React works and why it has become the go-to choice for modern web development.
React.js is an open-source JavaScript library that helps developers build dynamic and interactive user interfaces, primarily for single-page applications (SPAs). React follows a component-based architecture, where UI elements are broken down into independent, reusable components.
Unlike traditional JavaScript frameworks like Angular, React uses a virtual DOM (Document Object Model) for efficient rendering, making it faster and more scalable. React focuses only on the view layer of an application (the UI) and can be easily integrated with other libraries or frameworks.
React has gained massive popularity due to several unique features that make development easier and more efficient:
Component-Based Architecture
Virtual DOM
One-Way Data Binding
JSX (JavaScript XML)
Fast Rendering
Rich Ecosystem
Strong Community Support
React follows a well-defined architecture based on components, state management, and the virtual DOM.
React applications are made up of components, which are reusable UI elements. There are two main types of components:
Functional Components (Recommended)
function Welcome() {
return <h1>Hello, World!</h1>;
}
Class Components (Older method, still in use)
class Welcome extends React.Component {
render() {
return <h1>Hello, World!</h1>;
}
}
JSX is a syntax extension that allows developers to write HTML-like code inside JavaScript.
Example:
const element = <h1>Hello, React!</h1>;
JSX makes UI code more readable and is converted to pure JavaScript at runtime.
Props are used to pass data between components. They are read-only and cannot be modified by the receiving component.
Example:
function Greeting(props) {
return <h1>Hello, {props.name}!</h1>;
}
<Greeting name="John" />;
State is a built-in object that allows components to hold and manage data dynamically.
Example using the useState
hook:
import { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
React components have lifecycle methods that run at different stages of a component’s life. These are mostly used in class components.
Example:
class Example extends React.Component {
componentDidMount() {
console.log("Component Mounted");
}
render() {
return <h1>Hello, Lifecycle!</h1>;
}
}
Hooks are functions that let you use state and lifecycle features in functional components.
Popular hooks:
useState
– For managing state.useEffect
– For side effects like fetching data.useContext
– For global state management.useRef
– For referencing DOM elements.Example using useEffect
:
import { useState, useEffect } from 'react';
function Timer() {
const [seconds, setSeconds] = useState(0);
useEffect(() => {
const interval = setInterval(() => {
setSeconds((prev) => prev + 1);
}, 1000);
return () => clearInterval(interval);
}, []);
return <p>Timer: {seconds} seconds</p>;
}
Used for navigation in single-page applications.
Example:
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
function App() {
return (
<Router>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
</Switch>
</Router>
);
}
Redux helps manage global state efficiently.
Example using useSelector
and useDispatch
:
import { useSelector, useDispatch } from "react-redux";
function Counter() {
const count = useSelector(state => state.count);
const dispatch = useDispatch();
return (
<div>
<p>Count: {count}</p>
<button onClick={() => dispatch({ type: 'INCREMENT' })}>+</button>
</div>
);
}
Next.js enhances React applications by providing server-side rendering (SSR) and static site generation (SSG).
Example:
export async function getServerSideProps() {
return { props: { message: "Hello from server" } };
}
React is used in a wide range of applications:
React.js has revolutionized modern web development with its component-based architecture, virtual DOM, hooks, and strong ecosystem. Whether you're building a simple website or a complex web application, React provides the tools and flexibility needed for scalable, high-performance development.
With the rise of React frameworks like Next.js and state management tools like Redux, the future of React looks promising. If you're a developer looking to build fast, interactive, and scalable applications, React.js is the perfect choice!
Got any questions or want to share your React experience? Drop a comment below!