React Basics | A Beginnerβs Guide
πΉ What is React?
Section titled βπΉ What is React?βReact is a JavaScript library for building user interfaces. Developed by Facebook (Meta), it is widely used to create fast, scalable, and interactive web applications.
β
Component-Based β UI is built using reusable components.
β
Virtual DOM β Efficiently updates the UI for better performance.
β
Declarative β Simplifies UI development with a predictable state.
β
Used by Industry Leaders β Facebook, Instagram, Netflix, Airbnb, and more.
πΉ Installing React
Section titled βπΉ Installing ReactβTo create a new React app, use Create React App (CRA):
npx create-react-app my-appcd my-appnpm start
Or use Vite (for faster builds):
```shnpm create vite@latest my-app --template reactcd my-appnpm installnpm run dev
πΉ JSX: JavaScript + HTML
Section titled βπΉ JSX: JavaScript + HTMLβReact uses JSX (JavaScript XML) to write HTML-like code inside JavaScript.
const element = <h1>Hello, React!</h1>;ReactDOM.render(element, document.getElementById('root'));
β Looks like HTML but gets compiled to JavaScript. β Helps in creating UI components efficiently.
React Components A Component is a reusable UI element. React has two types of components:
- Functional Component (Modern Approach)
function Welcome() { return <h1>Hello, World!</h1>;}
- Class Component (Traditional Approach)
class Welcome extends React.Component { render() { return <h1>Hello, World!</h1>; }}
π‘ Best Practice: Use functional components with hooks for modern development.
πΉ Props in React
Section titled βπΉ Props in ReactβProps (short for Properties) allow components to receive dynamic data from parent components.
function Greeting(props) { return <h1>Hello, {props.name}!</h1>;}
<Greeting name="Alice" /> // Output: Hello, Alice!
β Props are read-only and cannot be modified inside the component.
πΉ State in React State allows components to manage and update data dynamically.
import { useState } from 'react';
function Counter() { const [count, setCount] = useState(0);
return ( <div> <p>Count: {count}</p> <button onClick={() => setCount(count + 1)}>Increase</button> </div> );}
β useState(0) β Initializes state with 0. β setCount(count + 1) β Updates the state.
πΉ Handling Events in React React events are similar to DOM events, but they use camelCase instead of lowercase.
function ClickButton() { function handleClick() { alert("Button clicked!"); }
return <button onClick={handleClick}>Click Me</button>;}
πΉ Conditional Rendering in React
Section titled βπΉ Conditional Rendering in ReactβUse if
statements or ternary operators to conditionally render components.
function Message({ isLoggedIn }) { return isLoggedIn ? <h1>Welcome Back!</h1> : <h1>Please Sign In</h1>;}
β
isLoggedIn ?
πΉ React Lists & Keys
Section titled βπΉ React Lists & KeysβWhen rendering lists, always use keys for better performance.
function ItemList() { const items = ["Apple", "Banana", "Orange"];
return ( <ul> {items.map((item, index) => ( <li key={index}>{item}</li> ))} </ul> );}
β Keys help React identify elements efficiently when updating lists.
πΉ React Hooks (Modern State Management) React Hooks allow functional components to use state and lifecycle features.
useState
(State Management)
const [count, setCount] = useState(0);
useEffect
(Side Effects, API Calls)
useEffect(() => { console.log("Component Mounted!");}, []);
β Replaces class lifecycle methods (componentDidMount, componentDidUpdate).
πΉ React Router (Navigation)
Section titled βπΉ React Router (Navigation)βTo enable page navigation in React, use React Router.
npm install react-router-dom
Example routing setup:
import { BrowserRouter as Router, Route, Routes, Link } from "react-router-dom";
function Home() { return <h1>Home Page</h1>;}
function About() { return <h1>About Page</h1>;}
function App() { return ( <Router> <nav> <Link to="/">Home</Link> <Link to="/about">About</Link> </nav> <Routes> <Route path="/" element={<Home />} /> <Route path="/about" element={<About />} /> </Routes> </Router> );}
β Routes manages different pages inside the app. β Link provides client-side navigation without reloading the page.
πΉ React vs Other Frontend Frameworks
Section titled βπΉ React vs Other Frontend FrameworksβFeature | React | Angular | Vue.js |
---|---|---|---|
Type | Library | Framework | Framework |
Learning Curve | Easy | Steep | Moderate |
State Management | Hooks (useState , useReducer ) | Services & RxJS | Vuex, Pinia |
Performance | Fast (Virtual DOM) | Moderate | Fast (Reactivity System) |
Used For | UI Components | Enterprise Apps | UI Components & Apps |
πΉ Getting Started with React Development
Section titled βπΉ Getting Started with React Developmentβ1οΈβ£ Install Node.js β Download from Node.js Official Site 2οΈβ£ Create a React App β npx create-react-app my-app 3οΈβ£ Start Development Server β npm start 4οΈβ£ Build for Production β npm run build
πΉ Conclusion
Section titled βπΉ ConclusionβReact is a powerful, flexible, and fast JavaScript library for building modern web applications. By mastering components, props, state, hooks, and routing, you can create scalable and interactive UIs with ease.
π Next Steps? Explore React Context API, Redux, and Advanced Hooks!