Command Palette

Search for a command to run...

Hectal
Phase 25: Frontend System Design with React
React · Exercise 1 of 5Next
Intermediate

Normalise a feed and update one entity

The fake API response in the starter gives every post its own copy of its author. Write normalise(posts) so the state is { postsById, usersById, ids }: each post stores authorId instead of an author object, and each user is stored once.

Write the reducer: { type: "rename", userId, name } changes one user; { type: "like", postId } adds 1 to one post's likes.

Render one <article className="post"> per id, in order, containing a <span className="author"> with the author's current name, a <p className="text"> with the text, and a like button with id={"like-" + id} whose text ends with the like count (for example ♥ 3).

Clicking #rename (already in the starter) must update the name on every post by that user.

What Check tests

  1. Three .post articles in the API's order
  2. Each post shows its author's name
  3. Renaming Asha updates both of her posts and not Ravi's
  4. Liking p2 adds one to p2 only
  5. The state is normalised (usersById and authorId)

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Your page

Press Run to see the page.

Console

Nothing logged yet.