
Karthick KarthickWhat is React? Why is it Used? Who Created It? And Understanding React Hooks Introduction React is...
What is React? Why is it Used? Who Created It? And Understanding React Hooks
Introduction
React is one of the most popular JavaScript libraries used for building modern web applications. From social media platforms to e-commerce websites, React powers thousands of applications worldwide because of its speed, flexibility, and component-based architecture.
In this article, we will explore what React is, why it is used, who created it, and some of the most important React Hooks such as useState, useEffect, useContext, useNavigate, useReducer, useRef, useMemo, and useCallback.
What is React?
React is an open-source JavaScript library used for building user interfaces (UI), especially for Single Page Applications (SPAs).
Instead of updating an entire webpage, React updates only the parts of the page that change, making applications faster and more efficient.
Key Features of React
Who Created React?
React was created by , a software engineer at "about.meta.com" (https://reference-url-citation.invalid/1).
Timeline
Why is React Used?
React became popular because it solves many challenges in traditional web development.
Developers can create a component once and use it multiple times.
Example:
function Welcome() {
return
React uses a Virtual DOM to update only changed elements instead of refreshing the entire page.
Applications are divided into smaller components, making code easier to manage.
Millions of developers contribute tutorials, libraries, and tools.
React works well with:
What are React Hooks?
Hooks are special functions introduced in React 16.8 that allow developers to use state and other React features inside functional components.
Before Hooks, these features were available only in class components.
The useState Hook is used to manage state in functional components.
Syntax
const [count, setCount] = useState(0);
Example
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<>
Use Cases
The useEffect Hook is used to perform side effects.
Examples:
Example
import { useEffect } from "react";
function App() {
useEffect(() => {
console.log("Component Mounted");
}, []);
return
Use Cases
The useContext Hook allows data sharing across multiple components without prop drilling.
Example
const UserContext = createContext();
function Home() {
const user = useContext(UserContext);
return
Use Cases
The useNavigate Hook is provided by React Router.
It allows navigation between pages programmatically.
Example
import { useNavigate } from "react-router-dom";
function Home() {
const navigate = useNavigate();
return (
navigate("/about")}>
Go To About
);
}
Use Cases
useReducer is used for complex state management.
Syntax
const [state, dispatch] = useReducer(reducer, initialState);
Example
function reducer(state, action) {
switch(action.type) {
case "increment":
return { count: state.count + 1 };
default:
return state;
}
}
Use Cases
useRef creates a mutable reference that persists across renders.
Example
import { useRef } from "react";
function InputFocus() {
const inputRef = useRef();
const focusInput = () => {
inputRef.current.focus();
};
return (
<>
Focus
</>
);
}
Use Cases
useMemo is used to optimize performance by memoizing expensive calculations.
Example
const result = useMemo(() => {
return heavyCalculation(data);
}, [data]);
Use Cases
useCallback memoizes functions to prevent unnecessary re-creation.
Example
const handleClick = useCallback(() => {
console.log("Clicked");
}, []);
Use Cases
Difference Between useMemo and useCallback
useMemo| useCallback
Memoizes values| Memoizes functions
Returns computed value| Returns function
Used for calculations| Used for event handlers
Advantages of React