Personal Catalogue: React & Firebase — Chapter 8, Exercise 2 ===================================================================== TASK Build the naive useDuplicateCheckNaive hook (no debounce, no staleness guard) and reproduce the race condition described in the chapter — with a real "Dune" book already saved, type "Dune Messiah" and observe a false "Dune already exists" warning appearing after the full title has been typed. SOLUTION import { useState, useEffect } from 'react'; import { searchItems } from '../search'; function useDuplicateCheckNaive(title) { const [match, setMatch] = useState(null); useEffect(() => { const trimmed = title.trim(); if (trimmed.length < 3) { setMatch(null); return; } searchItems(trimmed).then((results) => { const exact = results.find((r) => r.title.toLowerCase() === trimmed.toLowerCase()); setMatch(exact || null); }); }, [title]); return match; } Setup: a real Book titled exactly "Dune" already exists in the catalogue. Reproduction: type "Dune Messiah" into the title field at a normal typing pace (not pausing between "Dune" and " Messiah"). Observed sequence: 1. After "Dune" is typed, a call fires for "Dune". 2. Typing continues to "Dune Messiah" — a second, independent call fires for "Dune Messiah". 3. The "Dune Messiah" call resolves first (no exact match, since no book is titled exactly that): no warning shown. Correct, for the moment. 4. The earlier "Dune" call resolves late, after the field already reads "Dune Messiah": the warning "Dune already exists" appears, even though the field no longer says "Dune" at all. The warning is genuinely wrong at the moment it appears — there is no book titled "Dune Messiah," yet the UI claims a duplicate exists, purely because an earlier network response happened to arrive after a later one. WHY THIS WORKS AS AN ANSWER ---------------------------- It builds the exact naive hook the chapter describes, reproduces the real timing sequence rather than only describing it abstractly, and correctly identifies the observed warning as genuinely incorrect given the actual state of the catalogue at the moment it appears.