Fundamentals of React

Fundamentals of React

  • Profile picture of Mcs
  • by Mcs July 2, 2025

React is a JavaScript library for building user interfaces, and understanding its fundamentals is crucial for developing efficient and interactive web applications. Key concepts include:

1. Components (Building Blocks)

React apps are made of components – reusable, self-contained pieces of UI.

Functional Component:

function Welcome() {
 return <h1>Hello, React!</h1>;
}

Class Component (less common now):

class Welcome extends React.Component {
 render() {
   return <h1>Hello, React!</h1>;
 }
}

2. JSX (JavaScript XML)

JSX lets you write HTML inside JavaScript. It’s easier to read and write UI code.

const element = <h2>This is JSX</h2>;

3. Props (Component Inputs)

Props are how you pass data to components.

function Greet(props) {
 return <h1>Hello, {props.name}</h1>;
}

// Usage
<Greet name="MCS" />

4. State (Component Data)

useState() lets functional components have internal data that can change.

import { useState } from 'react';

function Counter() {
 const [count, setCount] = useState(0);

 return (
   <>
     <p>{count}</p>
     <button onClick={() => setCount(count + 1)}>+</button>
   </>
 );
}

5. Event Handling

React uses camelCase events like onClick, onChange, etc.
 

<button onClick={() => alert('Clicked!')}>Click Me</button>

6. Conditional Rendering

Use if, ternary ? :, or logical && to show UI conditionally.

{isLoggedIn ? <Dashboard /> : <Login />}

7. Lists and Keys

Use .map() to display lists and add a unique key prop.

const items = ['A', 'B', 'C'];
<ul>
 {items.map((item, index) => <li key={index}>{item}</li>)}
</ul>

8. useEffect Hook (Side Effects)

useEffect() is used for things like fetching data, setting up listeners, etc.

useEffect(() => {
 console.log('Component mounted!');
}, []);

9. Component Tree / Structure

React builds a tree of components — components can have parent/child relationships and communicate via props.

10. Virtual DOM

React creates a virtual copy of the DOM and updates it efficiently when changes occur — improving performance.

Summary Table

ConceptPurpose
JSXHTML inside JavaScript
ComponentsReusable UI blocks
PropsData from parent
StateLocal data that can change
Event HandlingHandle user interaction
Lists & KeysRender dynamic data with unique keys
useEffectHandle side effects
Virtual DOMFast UI updates

Comments

Add new comment

Restricted HTML

  • Allowed HTML tags: <br> <p> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id> <cite> <dl> <dt> <dd> <a hreflang href> <blockquote cite> <ul type> <ol type start> <strong> <em> <code> <li>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.