01 · Grid Sequence Selector
Code Walkthrough
Six steps from empty component to interruptible reverse animation. Expand a step to read the reasoning and copy the snippet.
A Set gives O(1) membership checks for the coloured squares; an array preserves the click order.
selected tracks active squares, sequence tracks the order of selections only, isReverting blocks input while the animation runs.
Reactconst [selected, setSelected] = useState(new Set()); const [sequence, setSequence] = useState([]); const [isReverting, setIsReverting] = useState(false);
Everything together
The finished component, same code as the Solution tab.
selected — 0 / 8
sequence — none
status — idle
- Click squares in any order (center is inert).
- Clicking again deselects without touching the sequence.
- Fill all 8 → auto-deselect in reverse, 200ms apart.