React JavaScript Framework
- coding z2m
- Nov 22, 2024
- 3 min read
Updated: Jan 22

React JavaScript Framework
What is React?
React JavaScript Framework library for building user interfaces, particularly for web applications. Developed and maintained by Facebook, React focuses on creating reusable UI components and managing the dynamic, ever-changing nature of web interfaces efficiently.
React enables developers to build interactive and responsive web applications where components can automatically update when data changes, ensuring seamless user experiences. How react components can automatically update when data changes? React components automatically update when data changes by using state and props. When the state of a component changes, React triggers a re-render to update the UI. Similarly, when props (data passed from a parent to a child component) change, the child component re-renders to reflect the new data. State Example:
In a shopping cart, the total number of items is stored in the component's state.
When a user adds a product, the state (cartItems) is updated, and React automatically updates the cart display.
const ShoppingCart = () => {
const [cartItems, setCartItems] = React.useState(0);
const addToCart = () => setCartItems(cartItems + 1);
return (
<div>
<p>Items in Cart: {cartItems}</p>
<button onClick={addToCart}>Add to Cart</button>
</div>
);
};
Props Example:
A ProductCard receives product data via props. If the parent component updates the product details, the ProductCard reflects the new data.
<ProductCard product={{ name: 'Laptop', price: 999 }} />
In both cases, React ensures the UI stays in sync with the underlying data without manual DOM manipulation.
How React Helps in Building UIs (With a Real-World Example)
Let’s consider a real-world example: an e-commerce website.
Reusable Components:
Imagine the website has a product listing page. Each product displayed can be built as a React Component (e.g., <ProductCard />). This reusable component can be used to display multiple products with unique data like images, names, and prices.
If the design of the product card changes, updating it in one place will reflect the changes across the entire site.
Dynamic Updates with State Management:
Virtual DOM for Efficiency:
React uses a Virtual DOM, which improves the performance of the application. For instance, when a user updates the quantity of a product in the cart, React only updates the specific part of the page, rather than re-rendering the entire UI.
Data Flow and Interactivity:
React’s one-way data flow makes it easier to debug and maintain code. For instance, the shopping cart's total price can be calculated in a parent component and passed as a prop to child components.
Code Example: A Simple Product Card
Here’s how React might render a product card:
import React from 'react';
const ProductCard = ({ product }) => {
return (
<div className="border rounded-lg p-4 shadow-sm">
<img src={product.image} alt={product.name} className="w-full h-48 object-cover" />
<h3 className="text-lg font-bold mt-2">{product.name}</h3>
<p className="text-gray-500 mt-1">${product.price}</p>
<button className="bg-blue-500 text-white py-1 px-4 mt-2 rounded hover:bg-blue-600">
Add to Cart
</button>
</div>
);
};
export default ProductCard;
Here, each product object (with properties like image, name, and price) is passed as a prop to the ProductCard component, which renders the UI dynamically.
Conclusion
React simplifies UI development by introducing reusable components, efficient updates with the Virtual DOM, and powerful state management. Whether you’re building an e-commerce platform, a social media app, or a dashboard, React ensures your application is modular, efficient, and interactive.
This makes React the go-to library for modern web development.
Comments