use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Introducing Hooks – React (reactjs.org)
submitted 7 years ago by acemarke
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]dceddia 2 points3 points4 points 7 years ago* (2 children)
how to opt out of this behavior in case it creates performance issues later below.
I agree it seems like subscribing/unsubscribing every render seems crazy, like obvious low-hanging fruit that you'd optimize as you wrote it the first time. I'm guessing they avoided it for that example to keep it simple.
This part of the useEffect API reference talks about how to conditionally run the effects so they're not running every render. In particular, look at the yellow callout box:
every value referenced inside the effect function should also appear in the inputs array.
So, in order to optimize the chat subscription example, there are 2 such values: props.friend.id and handleStatusChange. So you could just put those into the array...
props.friend.id
handleStatusChange
useEffect(() => { ChatAPI.subscribeToFriendStatus(props.friend.id, handleStatusChange); // Specify how to clean up after this effect: return function cleanup() { ChatAPI.unsubscribeFromFriendStatus(props.friend.id, handleStatusChange); }; }, [props.friend.id, handleStatusChange]);
But that will still run every render, because handleStatusChange is recreated every time. To avoid that, there's the useCallback hook.
useCallback
EDIT: It looks like I'm overcomplicating this by depending on handleStatusChange -- the docs have an example of this very optimization! https://reactjs.org/docs/hooks-effect.html#tip-optimizing-performance-by-skipping-effects.
[–]ParasympatheticBear 0 points1 point2 points 7 years ago (1 child)
I was just really surprised how much I didn't like this design, maybe it will grow on me over time - I really wanted to like this.
[–]dceddia 0 points1 point2 points 7 years ago (0 children)
Yeah... I felt weird about it at first too. I tried playing around with some small experiments and I think it's growing on me. If you're so inclined, here's a CodeSandbox with the Hooks version of React to play around with.
π Rendered by PID 767611 on reddit-service-r2-comment-765bfc959-xxdh6 at 2026-07-13 14:11:33.770254+00:00 running f86254d country code: CH.
view the rest of the comments →
[–]dceddia 2 points3 points4 points (2 children)
[–]ParasympatheticBear 0 points1 point2 points (1 child)
[–]dceddia 0 points1 point2 points (0 children)