Fetching data in React without a library means juggling useEffect, abort controllers, isLoading booleans, error state, and cache invalidation โ all by hand. SWR, TanStack Query, and RTK Query solve all of that out of the box. This challenge has the grid and components pre-built โ your job is to wire up SWR and bring the GitHub-style heatmap to life.
๐งฉ Overview
Fetch contribution data for a GitHub-style heatmap using SWR. The grid and components are already built โ you wire up data fetching with loading skeletons, error handling, and year navigation to make it work.
๐ https://www.reactchallenges.com/challenges/github-contribution-heatmap
โ Requirements
- Fetch contribution data from
https://example-heatmap?year={year} - Write a custom fetcher that checks
res.ok, parses the error body, and throws on failure โ SWR won't know a request failed unless the fetcher throws - Display a loading skeleton while data is being fetched
- Display an error skeleton with the server's error message when the request fails
- Display the heatmap grid with flattened contributions on success
- Allow switching between years (2023โ2026) with interactive buttons
- Highlight the currently selected year button
- Calculate the offset so January 1st aligns with the correct weekday column
๐ก Notes
-
contributionsis a 2D array (weeks ร days) โ flatten it with.flat()before passing toHeatmapGrid - The mock API has a random 300-500ms delay and fails ~20% of the time
- The custom fetcher pattern is universal: SWR, TanStack Query, and RTK Query all require you to throw on non-ok responses
- The data is from Linus Torvalds' real GitHub contributions โ every cell reflects actual commits, merges, and pull requests
๐งช Tests
- Renders the app title
- Shows skeleton while loading
- Displays heatmap grid with contributions when data loads
- Shows skeleton with error when request fails
- Highlights 2026 as the selected year button
- Fetches new data when switching to a different year
- Aligns January 1st to the correct weekday for each year
Whether you're new to SWR or already using TanStack Query, this challenge reinforces the fetch-then-render pattern that every production React app relies on. Plus, you get to see Linus Torvalds' actual commit history rendered as a heatmap โ pretty cool.
Top comments (0)