Re:Making Sense of React Server Components
by orangepanda on Wednesday September 06, 2023 @04:24PM
(#37410784)
You dont have to specify the dependency array, it’s an optional optimisation.
[ Parent
| on HN ]
Re:Making Sense of React Server Components
by tuukkah on Wednesday September 06, 2023 @04:52PM
(#37411149)
Effects 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.
Re:Making Sense of React Server Components
by shortcake27 on Thursday September 07, 2023 @02:54AM
(#37415588)
This 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.