← Back to Course Overview

Code-Alongs - Web Unit 2 Sprint 6

Code-Along 1: Building a React Component Library with useState and Props

Follow along as we build a reusable component library using React and styled-components, focusing on useState for state management and props for component communication.

What You'll Learn

What You'll Build

In this code-along, you'll build a component library that includes:

// Example of what you'll create:
import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);
  
  return (
    <div className="counter">
      <h2>Counter: {count}</h2>
      <Button onClick={() => setCount(count - 1)}>Decrease</Button>
      <Button primary onClick={() => setCount(count + 1)}>Increase</Button>
      <Button secondary onClick={() => setCount(0)}>Reset</Button>
    </div>
  );
}

Note: Please visit the platform to access the current code-along materials and videos.

Code-Along 2: Advanced State Management with useEffect and APIs

Learn advanced state management techniques using the useEffect hook in React, including data fetching, handling side effects, and working with external APIs.

What You'll Learn

What You'll Build

In this code-along, you'll build a data-driven application that includes:

// Example of what you'll create:
import React, { useState, useEffect } from 'react';

function UserDirectory() {
  const [users, setUsers] = useState([]);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(null);
  const [search, setSearch] = useState('');
  
  useEffect(() => {
    async function fetchUsers() {
      try {
        setLoading(true);
        const response = await fetch('https://api.example.com/users');
        if (!response.ok) throw new Error('Failed to fetch');
        const data = await response.json();
        setUsers(data);
        setError(null);
      } catch (err) {
        setError(err.message);
      } finally {
        setLoading(false);
      }
    }
    
    fetchUsers();
  }, []); // Empty dependency array means this runs once on mount
  
  // Filter users based on search term
  const filteredUsers = users.filter(user => 
    user.name.toLowerCase().includes(search.toLowerCase())
  );
  
  return (
    <div>
      <h1>User Directory</h1>
      <input 
        type="text" 
        placeholder="Search users..." 
        value={search}
        onChange={(e) => setSearch(e.target.value)}
      />
      
      {loading && <p>Loading users...</p>}
      {error && <p>Error: {error}</p>}
      
      <ul>
        {filteredUsers.map(user => (
          <li key={user.id}>{user.name} - {user.email}</li>
        ))}
      </ul>
    </div>
  );
}

Note: Please visit the platform to access the current code-along materials and videos.

Additional Resources