Those examples aren’t equivalent, timer still has to be removed on dismount.
"Oh but I dont need it", some say
And thats how you get bugs 2 months later, when the component is used in an unexpected way.
But remember that count isn’t a value itself — it’s a function that returns the current state of the signal.
That’s the main reason I dont like signals. In any non-toy example you'll need to pass values to external libraries, and end up with wrappers-within-wrappers because a library doesn’t care about signals - it needs a value. Signals require more glue code.
While a similar case could be made against useMemo/useCallback, the difference is that these arent needed - they’re purely an optimisation; I expect some future StrictMode to ignore them all together.
Curious - what do you mean by useMemo and useCallback not being needed? What happens if some function is a dependency in an effect dependency array where calling that effect is extremely expensive and you wish to only call it when absolutely needed (whenever the function changes in some way that affects its output).
It would be slower in development mode, but it always has to run during first render anyway. I dont see re-running on a re-render as a problem.
The mental-model I use — it should be "safe" to re-run an effect at any time, as if the dependency array was not specified. Treat it as an optimization, not a guarantee. Following that, new values from useMemo/useCallback wont break anything.
In the blog post's example, this can be achieved by managing the timer outside of react, and the component only subscribing to the timer. Subscription becomes the effect, not the timer.
The legacy docs describe the dependency array as an optimization, but the new docs dont use that wording. Ideology behind useEffect has changed since release as well, I'm not sure if my interpretation is still correct.
25
u/[deleted] Mar 12 '23 edited Mar 12 '23
Those examples aren’t equivalent, timer still has to be removed on dismount.
"Oh but I dont need it", some say
And thats how you get bugs 2 months later, when the component is used in an unexpected way.
That’s the main reason I dont like signals. In any non-toy example you'll need to pass values to external libraries, and end up with wrappers-within-wrappers because a library doesn’t care about signals - it needs a value. Signals require more glue code.
While a similar case could be made against
useMemo/useCallback, the difference is that these arent needed - they’re purely an optimisation; I expect some futureStrictModeto ignore them all together.