Posts

React Hooks

Image
 Introduction Before React Hooks were introduced, managing state and lifecycle methods was only possible using class components . This often resulted in long, complex code that was read and maintain. React Hooks, introduced in React 16.8, completely changed how developers write React components. Hooks allow you to use powerful React features like state, side effects, and easier to understand. Most commonly used hooks useState useEffect What Are React Hooks? Hooks are special functions provided by React that let you "hook into"  React features. With Hooks, you can Store and update state Run code when a component loads or updates Fetch data from APIs Interact with the browser (like updating the document title) Hooks only work in functional components and must follow 2 simple rules 1. Call Hooks at the top level of your component 2. Only call Hooks from React functions Understanding useState The useState hook allows you to add state to a functional component Basic Syntax stateV...

State in React Using useState Hook

Image
 Introduction In React, state is used to store data that can change overtime. When this data changes, React automatically updates the UI. In older React versions, state was mostly used in class components. But in modern React, we use Hooks, and the most important hook for state is called "useState". What is State in React? State is:     Data that belongs to a component     Data that can change     Data that controls what is shown on the screen Example of state: Counter value Input text Login status Theme (dark / Light) When state changes, React re-renders the component automatically. What is the useState Hook ? useState is a React Hook that allows you to: Create state variables Update state values Re-render the UI when state changes Basic Syntax stateValue - current value of the state setStateValue - function used to update the state initialValue - starting value of the state Importing useState Before using useState, you must import it from React S...

Props in React

Image
Introduction to React Props In React, Props are used to pass data from one component to another. They help make components reusable, dynamic and easy to manage . Why Props Are Important Props allow to: Reuse the same component with different data Pass data from parent to child components Keep components flexible and clean Build dynamic user interfaces Without props, every component would show hardcoded data, which is not paractical and unusual. Basic Example of Props Parent Component & Child Component Props vs State Props   Passed from parent Read Only Used for display Immutable State Managed inside component Can change Used for interaction Mutable Default Props Default props are used when no value is passed Children Prop React automatically provides a special prop called children Common Mistakes With Props Changing props directly Forgetting to pass required props Using props instead of state for changing data Overloading a component with too many props When to Use Props Use pr...

JSX in React

Image
 JSX stands for JavaScript XML. It allows you to write HTML like code inside JavaScript. JSX makes React code easier to read and understand.Behind the scenes, JSX is converted into JavaScript. JSX must always return a single parent element, such as a div or a fragment. Basic JSX Example JSX with JavaScript Expression JSX Must Return a Single Parent Element Wrong Exmple Code - Without a parent Element  Correct Code (Wrapped in a div) Correct - Using React Fragment JSX Behind the Scenes - Converted to JavaScript

React Components Explained with Simple Examples

Image
 Component are the building blocks of React Applications. A component is a JavaScript function that returns JSX. Components helps you reuse and keep the application modular. In modern React, funtional components are used instead of class components.

Understanding React Project Structure

  A modern React Project has a clean and simple structure. The src folder contains most of the application code. The main.jsx file contains React to the browser. The App.jsx file is the main component.

Setting Up React in 2026 Using Vite step by step

Image
 In 2026, the recommended way to start a React project is using Vite. Vite is faster and simpler than older tools. 1. First install Node.js  - You can choose your os (windows/Linux/Mac) and install 2. Then run the following commands: npm create vite@latest my-react-app cd my-react-app npm install npm run dev After running these commands, your React app will start on a local server.