Component Model
Component Model
React uses a component-based architecture where UIs are built from small, reusable pieces called components.
What is a Component?
A component is a self-contained piece of UI that can:
- Accept inputs (props)
- Manage internal state
- Render UI
- Be composed with other components
Component Example
function Welcome({ name }) {
return <h1>Hello, {name}!</h1>;
}
// Usage
<Welcome name="Alice" />
<Welcome name="Bob" />
Benefits
- Reusability: Use components across your app
- Separation of concerns: Each component handles one thing
- Testability: Components can be tested in isolation
- Maintainability: Changes are localized to components
Declarative UI
Declarative UI
React is declarative—you describe what the UI should look like, not how to update it.
Imperative vs Declarative
// Imperative (vanilla JS)
const element = document.createElement("div");
element.textContent = "Hello";
element.className = "greeting";
document.body.appendChild(element);
// When state changes
if (isVisible) {
element.style.display = "block";
} else {
element.style.display = "none";
}
// Declarative (React)
function Greeting({ name, isVisible }) {
if (!isVisible) return null;
return <div className="greeting">Hello, {name}!</div>;
}
// Just call it again when state changes
<Greeting name="Alice" isVisible={true} />
Benefits
- Easier to understand: Code reads like a description of UI
- Less bugs: No manual DOM manipulation
- Better state management: UI automatically reflects state
Virtual DOM
Virtual DOM
React uses a Virtual DOM to efficiently update the real DOM.
How It Works
- State changes trigger a re-render
- React creates a Virtual DOM tree (lightweight copy)
- React diffs the new tree against the previous one
- React reconciles only the changed parts to the real DOM
Benefits
- Performance: Minimizes expensive DOM operations
- Batching: Multiple state changes are batched into one update
- Cross-platform: Virtual DOM enables React Native
Simple Example
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>
Increment
</button>
</div>
);
}
When count changes:
- React creates new Virtual DOM with updated count
- React compares with previous Virtual DOM
- Only the
<p>tag's text content is updated - The button and div are not touched
Practice Problems
Create a reusable React component implementing Why React. Include proper state management and accessibility.
Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where neededWrite unit and integration tests for Why React using React Testing Library.
Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility testsOptimize Why React for performance. Consider memoization, code splitting, and bundle size.
Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysisQuiz
1. What is the core concept of React's architecture?
2. What does 'declarative' mean in React?
3. What is the Virtual DOM?
4. Why is React's approach better than manual DOM manipulation?
Flashcards
Question
What is a React component?
Click to reveal answer
Answer
A reusable piece of UI that accepts props and returns JSX
Question
What is declarative UI?
Click to reveal answer
Answer
Describing what the UI should look like, not how to update it
Question
What is the Virtual DOM?
Click to reveal answer
Answer
A lightweight copy of the real DOM for efficient updates
Question
What is reconciliation?
Click to reveal answer
Answer
The process of comparing Virtual DOM trees and updating the real DOM
Question
What is Why React?
Click to reveal answer
Answer
Why React is a key concept in frontend development.
Revision Notes
Key Takeaways
- 1.React uses a component-based architecture
- 2.Declarative UI describes what the UI should look like
- 3.Virtual DOM enables efficient updates
- 4.Components are reusable and composable
- 5.React handles DOM manipulation automatically
Interview Tips
- •Explain the difference between imperative and declarative approaches
- •Describe how the Virtual DOM works
- •Discuss benefits of component-based architecture
Cheat Sheet
Cheat Sheet
Component Model
- Reusable UI pieces
- Accept props, manage state
- Can be composed together
Declarative UI
- Describe what UI should look like
- React handles DOM updates
- Less manual DOM manipulation
Virtual DOM
- State changes trigger re-render
- New Virtual DOM created
- Diff against previous tree
- Update only changed parts
Benefits
- Reusability
- Maintainability
- Performance
- Testability