Fixing Missing Dependencies: Remove the Need
You'll learn how to solve dependency warnings by refactoring your code so the effect doesn't need certain values in the first place.
The Alternative Strategy
In the previous lesson, you learned to fix missing dependencies by adding them to the array. But there's another approach: eliminate the dependency entirely. If your effect doesn't reference a value, you don't need to include it—no warning, no bug.
Think of it like packing for a trip. Option 1: pack everything you might use. Option 2: change your plans so you don't need those items at all.
Common Refactoring Techniques
Move non-reactive logic outside the component: If a value doesn't depend on props or state, define it outside. It won't change between renders, so it's not a dependency.
Move calculations inside the effect: Instead of reading a computed value from state, perform the calculation directly inside the effect using only the raw values you already have as dependencies.
Use refs for values that shouldn't trigger re-runs:
If you need to read a value but don't want changes to it to re-run the effect, store it in a useRef. Refs aren't reactive—reading someRef.current doesn't create a dependency.
Extract logic that doesn't need reactive values: Sometimes a helper function references props unnecessarily. Pass only what it truly needs, or inline it where the dependencies are clearer.
Why This Matters
Removing unnecessary dependencies makes effects more predictable and performant. Fewer dependencies mean fewer re-runs, and simpler dependency arrays are easier to understand and maintain.
Key Takeaway: Before adding a missing dependency, ask: "Can I refactor so this effect doesn't need this value at all?" Moving logic, using refs, or restructuring calculations often eliminates dependencies cleanly.