Skip to main content

Lesson 6 of 7

Mini Project: Concurrent GitHub Feed

Build a GitHub activity feed that demonstrates all React 18 concurrent features: useSuspenseQuery for data, useTransition for non-urgent filter updates, useDeferredValue for search results, and Suspense boundaries for granular loading states.

50 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Concurrent GitHub Activity Feed

Concurrent GitHub Activity Feed
import { useState, useTransition, useDeferredValue, Suspense } from 'react';
import { useSuspenseQuery } from '@tanstack/react-query';

// ---- Types ----
interface GHEvent {
  id: string;
  type: string;
  actor: { login: string; avatar_url: string };
  repo: { name: string };
  created_at: string;
  payload: Record<string, unknown>;
}

// ---- API ----
async function fetchUserEvents(username: string): Promise<GHEvent[]> {
  const res = await fetch(`https://api.github.com/users/${username}/events/public?per_page=30`);
  if (!res.ok) throw new Error('GitHub API error: ' + res.status);
  return res.json();
}

// ---- Suspending data component ----
function EventFeed({ username, typeFilter }: { username: string; typeFilter: string }) {
  const { data: events } = useSuspenseQuery({
    queryKey: ['events', username],
    queryFn: () => fetchUserEvents(username),
    staleTime: 60_000,
  });

  const filtered = typeFilter === 'all'
    ? events
    : events.filter(e => e.type === typeFilter);

  if (filtered.length === 0) {
    return <div style={{ color: '#64748b', textAlign: 'center', padding: 40 }}>No {typeFilter} events found.</div>;
  }

  return (
    <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
      {filtered.map(event => (
        <EventCard key={event.id} event={event} />
      ))}
    </div>
  );
}

// ---- Event card ----
function EventCard({ event }: { event: GHEvent }) {
  const eventEmoji: Record<string, string> = {
    PushEvent: '๐Ÿ“ฆ', PullRequestEvent: '๐Ÿ”€', IssuesEvent: '๐Ÿ›',
    WatchEvent: 'โญ', ForkEvent: '๐Ÿด', CreateEvent: 'โœจ',
    DeleteEvent: '๐Ÿ—‘๏ธ', ReleaseEvent: '๐Ÿš€',
  };

  return (
    <div style={{ display: 'flex', alignItems: 'center', gap: 12, background: '#1e293b', borderRadius: 10, padding: '12px 16px', border: '1px solid #334155' }}>
      <img src={event.actor.avatar_url} alt="" style={{ width: 36, height: 36, borderRadius: '50%' }} />
      <div style={{ flex: 1 }}>
        <div style={{ fontSize: 13 }}>
          <strong>{event.actor.login}</strong>
          {' '}
          <span style={{ color: '#94a3b8' }}>
            {eventEmoji[event.type] ?? '๐Ÿ“'} {event.type.replace('Event', '')} in{' '}
          </span>
          <a href={`https://github.com/${event.repo.name}`} target="_blank" rel="noopener noreferrer" style={{ color: '#60a5fa' }}>
            {event.repo.name}
          </a>
        </div>
        <div style={{ fontSize: 11, color: '#475569', marginTop: 2 }}>
          {new Date(event.created_at).toLocaleString()}
        </div>
      </div>
    </div>
  );
}

// ---- Skeleton ----
function FeedSkeleton() {
  return (
    <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
      {Array.from({ length: 8 }).map((_, i) => (
        <div key={i} style={{ background: '#1e293b', borderRadius: 10, padding: '12px 16px', border: '1px solid #334155', display: 'flex', alignItems: 'center', gap: 12 }}>
          <div style={{ width: 36, height: 36, borderRadius: '50%', background: '#334155' }} />
          <div style={{ flex: 1 }}>
            <div style={{ width: '60%', height: 13, background: '#334155', borderRadius: 4, marginBottom: 6 }} />
            <div style={{ width: '30%', height: 11, background: '#1e293b', borderRadius: 4 }} />
          </div>
        </div>
      ))}
    </div>
  );
}

// ---- Main App ----
const EVENT_TYPES = ['all', 'PushEvent', 'PullRequestEvent', 'IssuesEvent', 'WatchEvent', 'ForkEvent'];

export default function GHActivityFeed() {
  const [username, setUsername] = useState('torvalds');
  const [inputValue, setInputValue] = useState('torvalds');
  const [typeFilter, setTypeFilter] = useState('all');
  const [isPending, startTransition] = useTransition();

  // Defer the username that triggers re-fetch (not the input value)
  const deferredFilter = useDeferredValue(typeFilter);

  function handleUsernameSubmit(e: React.FormEvent) {
    e.preventDefault();
    startTransition(() => setUsername(inputValue));
    // โ†’ EventFeed re-fetches for new username in a transition
    // โ†’ Input field is NEVER blocked
  }

  function handleTypeChange(type: string) {
    startTransition(() => setTypeFilter(type));
    // โ†’ List re-filters in transition, UI stays responsive
  }

  return (
    <div style={{ minHeight: '100vh', background: '#0f172a', color: 'white', fontFamily: 'system-ui', padding: 24, maxWidth: 680, margin: '0 auto' }}>
      <h1 style={{ margin: '0 0 4px', fontSize: 22 }}>โšก GitHub Activity Feed</h1>
      <p style={{ color: '#64748b', margin: '0 0 20px', fontSize: 13 }}>React 18 Concurrent Features Demo</p>

      {/* Username input */}
      <form onSubmit={handleUsernameSubmit} style={{ display: 'flex', gap: 8, marginBottom: 16 }}>
        <input
          value={inputValue}
          onChange={e => setInputValue(e.target.value)}
          placeholder="GitHub username"
          style={{ flex: 1, background: '#1e293b', border: '1px solid #334155', color: 'white', padding: '10px 14px', borderRadius: 8, fontSize: 14 }}
        />
        <button type="submit" disabled={isPending} style={{ background: '#229c6b', border: 'none', color: 'white', padding: '10px 20px', borderRadius: 8, cursor: 'pointer', fontWeight: 600, fontSize: 14 }}>
          {isPending ? 'Loading...' : 'Load'}
        </button>
      </form>

      {/* Type filter - uses useTransition */}
      <div style={{ display: 'flex', gap: 6, flexWrap: 'wrap', marginBottom: 16 }}>
        {EVENT_TYPES.map(type => (
          <button
            key={type}
            onClick={() => handleTypeChange(type)}
            style={{
              padding: '5px 12px', borderRadius: 999, fontSize: 12, fontWeight: 600, cursor: 'pointer',
              background: typeFilter === type ? '#229c6b' : '#1e293b',
              border: `1px solid ${typeFilter === type ? '#229c6b' : '#334155'}`,
              color: typeFilter === type ? 'white' : '#94a3b8',
            }}
          >
            {type === 'all' ? 'All' : type.replace('Event', '')}
          </button>
        ))}
      </div>

      {/* Suspense boundary - shows skeleton during initial load */}
      {/* isPending dims the list during transition (non-urgent update) */}
      <div style={{ opacity: isPending ? 0.6 : 1, transition: 'opacity 0.15s' }}>
        <Suspense fallback={<FeedSkeleton />}>
          <EventFeed username={username} typeFilter={deferredFilter} />
        </Suspense>
      </div>
    </div>
  );
}

Concurrent Features Demonstrated

  • useSuspenseQuery - EventFeed suspends while fetching; no loading state needed inside the component
  • Suspense boundary - FeedSkeleton shown during initial fetch and username change
  • useTransition - username change and type filter change are marked as non-urgent transitions; isPending gives subtle opacity feedback
  • useDeferredValue - typeFilter is deferred so filtering doesn't block the button click interaction
  • startTransition for fetch trigger - the username change that causes a re-fetch is wrapped in startTransition so typing in the input is never interrupted
  • Automatic batching - multiple state updates inside handleUsernameSubmit are batched into a single render

Tip

Tip

Practice Mini Project Concurrent GitHub Feed in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagramโ€ฆ

Visual architecture and flow for Mini Project: Concurrent GitHub Feed

Practice Task

Note

Practice Task - (1) Write a working example of Mini Project Concurrent GitHub Feed from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Mini Project Concurrent GitHub Feed is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.

Key Takeaways

  • Build a GitHub activity feed that demonstrates all React 18 concurrent features: useSuspenseQuery for data, useTransition for non-urgent filter updates, useDeferredValue for search results, and Suspense boundaries for granular loading states.
  • useSuspenseQuery - EventFeed suspends while fetching; no loading state needed inside the component
  • Suspense boundary - FeedSkeleton shown during initial fetch and username change
  • useTransition - username change and type filter change are marked as non-urgent transitions; isPending gives subtle opacity feedback

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/7 done