Lesson 11 of 49 intermediate

React — The UI Wizard

Building Interfaces Like LEGO

Open interactive version (quiz + challenge)

Real-world analogy

React is like a TV studio control room. Each screen (component) shows a different camera feed — weather, sports ticker, breaking news banner. The director (React) decides which screens update and when. Swap the weather panel for traffic? Only that screen changes — the rest keep running live!

What is it?

React is a JavaScript library for building user interfaces with components. Components are functions that return JSX (HTML-like syntax). State lets components remember data. Props pass data between components. React's virtual DOM makes updates fast.

Real-world relevance

React powers Facebook, Instagram, Netflix, Airbnb, and thousands more apps. It's the most popular frontend library with millions of developers worldwide.

Key points

Code example

// A simple React component — it's just a function! 🎯
import { useState } from 'react';

function LikeButton() {
  // State = component's memory
  const [likes, setLikes] = useState(0);
  const [emoji, setEmoji] = useState('👍');

  const handleClick = () => {
    setLikes(likes + 1);
    setEmoji(likes >= 10 ? '🔥' : likes >= 5 ? '❤️' : '👍');
  };

  return (
    <button onClick={handleClick}>
      {emoji} {likes} {likes === 1 ? 'like' : 'likes'}
    </button>
  );
}

// Using components together — like LEGO! 🧱
function App() {
  return (
    <div>
      <h1>My Awesome App</h1>
      <LikeButton />
      <LikeButton />  {/* Each has its own state! */}
    </div>
  );
}

// Props — pass data to components
interface ButtonProps {
  label: string;
  color: "blue" | "red" | "green";
  onClick: () => void;
}

function Button({ label, color, onClick }: ButtonProps) {
  return (
    <button
      style={{ background: color }}
      onClick={onClick}
    >
      {label}
    </button>
  );
}

// Using with props
<Button label="Click me!" color="blue" onClick={() => console.log('clicked')} />

// useEffect — run code on mount/update
function UserProfile({ userId }: { userId: string }) {
  const [user, setUser] = useState(null);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    // This runs when component mounts OR when userId changes
    fetch(`/api/users/${userId}`)
      .then(res => res.json())
      .then(data => {
        setUser(data);
        setLoading(false);
      });
  }, [userId]);  // dependency array — re-run if userId changes

  if (loading) return <p>Loading...</p>;
  return <div>Hello {user.name}</div>;
}

Line-by-line walkthrough

  1. 1. A simple React component — it's just a function! 🎯
  2. 2. Importing required dependencies
  3. 3.
  4. 4. Declaring a function
  5. 5. State = component's memory
  6. 6. Declaring a variable
  7. 7. Declaring a variable
  8. 8.
  9. 9. Declaring a variable
  10. 10.
  11. 11.
  12. 12. Closing block
  13. 13.
  14. 14. Returning a value
  15. 15.
  16. 16.
  17. 17.
  18. 18. Closing expression
  19. 19. Closing block
  20. 20.
  21. 21. Using components together — like LEGO! 🧱
  22. 22. Declaring a function
  23. 23. Returning a value
  24. 24.
  25. 25.
  26. 26.
  27. 27.
  28. 28.
  29. 29. Closing expression
  30. 30. Closing block
  31. 31.
  32. 32. Props — pass data to components
  33. 33. Defining an interface shape
  34. 34.
  35. 35.
  36. 36.
  37. 37. Closing block
  38. 38.
  39. 39. Declaring a function
  40. 40. Returning a value
  41. 41.
  42. 42.
  43. 43.
  44. 44.
  45. 45.
  46. 46.
  47. 47. Closing expression
  48. 48. Closing block
  49. 49.
  50. 50. Using with props
  51. 51.
  52. 52.
  53. 53. useEffect — run code on mount/update
  54. 54. Declaring a function
  55. 55. Declaring a variable
  56. 56. Declaring a variable
  57. 57.
  58. 58.
  59. 59. This runs when component mounts OR when userId changes
  60. 60.
  61. 61. Method chaining on the previous expression
  62. 62. Method chaining on the previous expression
  63. 63.
  64. 64.
  65. 65.
  66. 66.
  67. 67.
  68. 68. Conditional check
  69. 69. Returning a value
  70. 70. Closing block

Spot the bug

function Counter() {
  let count = 0;
  const increment = () => { count += 1; };
  return <button onClick={increment}>{count}</button>;
}
Need a hint?
How should you store data that changes in React?
Show answer
Using a regular variable (let count = 0) won't cause React to re-render when changed. Fix: use useState: const [count, setCount] = useState(0); and setCount(count + 1) in increment.

Explain like I'm 5

React is like building with LEGO! Each block is a piece of your website - a button block, a picture block, a text block. Snap them together to make a page. Want to change just the button? Swap that one block - everything else stays the same!

Fun fact

React was first used on Facebook's News Feed in 2011 and Instagram in 2012 — BEFORE it was released to the public in 2013! 🕵️

Hands-on challenge

Create a counter component with + and - buttons. Bonus: add a reset button and make the number turn red when negative!

More resources

Open interactive version (quiz + challenge) ← Back to course: Full-Stack Playbook