3 ms·
You dont have to specify the dependency array, it’s an optional optimisation.
by orangepanda 3y ago
You dont have to specify the dependency array, it’s an optional optimisation.
- tuukkah 3y agoEffects are not required to be idempotent, so it's not just an optimisation. React documentation has an example where the effect establishes a network connection. Even if there's time to connect between renders, the remote system doesn't necessarily handle such short-lived connections.
- shortcake27 3y agoThis isn’t quite true. If your effect depends on state, that state must be included as a dependency. This is easy to miss, so you install an eslint plugin to warn you about missing dependencies. This causes the opposite problem - adding dependencies unnecessarily to silence the warning. Then you need to ensure those dependencies are stable to avoid running the hook on every render. Or ignore the warning with a comment, and you’re back to square one. It’s all too easy to write an effect that either doesn’t run when it should, or runs when it shouldn’t. This is especially true for inexperienced developers.