mediumReact#145

Normalized entities reducer

Prompt

APIs return arrays; stores want lookup tables. Write entitiesReducer(state, action) over { byId: {}, allIds: [] }:

  1. { type: 'ADD', entity } — entity has an id; adds to both structures (ignore if id exists)
  2. { type: 'UPDATE', id, changes } — shallow-merges changes into the entity (no-op if missing)
  3. { type: 'REMOVE', id } — removes from both structures
  4. Pure — new objects on every change, allIds preserves insertion order

Hints

Solution

Your Code

15 min

Tests

Click Run to test your code