Week 12

TanStack Query

Optimistic updates

Authentication

Data persistance

Security Fundamentals

Practice

Assignment

Frontend Track

Week 12 Assignment

Task 1

Update your portfolio pages from fetching data using useEffect and useState and replace them with useQuery from TanStack Query.

After the refactor:

Task 2

The current theme toggle uses useState and the choice resets on every reload. Add localStorage persistence and keep it SSR-safe now that the toggle lives inside a Next.js Client Component.

The implementation must:

Verify in DevTools → Application → Local Storage that the value is written, and confirm the choice survives both a page reload and closing/reopening the browser.

Task 3

Add a like button with a visible count to each item on the /projects page you built in Task 1. The count must update in the UI the instant the button is clicked - before the network request finishes.

Use useMutation with all three lifecycle callbacks:

You can add a new field in your data source to get the initial value side. You don't need a real backend for the updating the count. A mutationFn that resolves after a short delay is enough:

mutationFn: async (projectId: number) => {
  await new Promise(r => setTimeout(r, 500))
}

To verify the rollback, temporarily change the mutationFn to throw after the delay:

mutationFn: async (projectId: number) => {
  await new Promise(r => setTimeout(r, 800))
  throw new Error('Simulated failure')
}

Click the button. The count must increment the moment you click, then decrement 800ms later when onError fires.

Submission

Follow the Assignment submission guide to learn how to submit the assignment.


The HackYourFuture curriculum is licensed under CC BY-NC-SA 4.0 *https://hackyourfuture.net/*

CC BY-NC-SA 4.0 Icons

Built with ❤️ by the HackYourFuture community · Thank you, contributors

Found a mistake or have a suggestion? Let us know in the feedback form.