Co-locating Related State Before Extracting
You'll learn why grouping tightly coupled state and effects together in one place helps you identify whether extraction into a custom hook actually makes sense.
The Pre-Extraction Organization Step
Before you extract logic into a custom hook, take a moment to co-locate all the related pieces in one spot within your component. Move related useState calls, useEffect blocks, and helper functions next to each other. This simple organizational step reveals the true boundaries of your logic.
Think of it like cleaning your desk before deciding what goes in a drawer. When everything's scattered, you might stuff unrelated items together. When everything's grouped first, you see clear categories emerge.
Why Co-location Comes First
When related state is scattered throughout a component—some at the top, effects in the middle, handlers at the bottom—it's hard to judge if they form a cohesive unit worth extracting. By moving them together, you can ask:
- Do these pieces truly depend on each other?
- Is there a clear input/output boundary?
- Would this make sense as a standalone piece of logic?
If your co-located block has tentacles reaching into unrelated component concerns, extraction will be messy. But if it forms a neat, self-contained cluster, you've found a good extraction candidate.
The Visual Test
Co-located code should read like a paragraph about one thing. If you need blank lines or comments to separate "the form state stuff" from "the validation stuff" from "the submission stuff" within your group, those might be three hooks—or they might need to stay together because they're actually one complex concern.
Key Takeaway: Co-locate related state, effects, and handlers together in your component first; this grouping reveals whether you have cohesive, extractable logic or intertwined concerns better left inline.