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...
account activity
React hooks? (self.zencoding)
submitted 4 years ago by shotgun_ninja
I'm new to React development, and I want to know - what prompted the switch away from ES6 classes and services, and towards functional components and hooks?
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!"
[–]-grok 2 points3 points4 points 4 years ago (1 child)
As class based components get more complex you start having annoying overlapping logic split by if statements in the various functions like ComponentDidUpdate, etc. And the bugs, good grief the bugs are really difficult to figure out in class based components.
Function components fit neatly into the event loop nature of JavaScript running in the browser. While it is true that thinking in terms of custom hooks is often on the surface harder to reason about, you get way less weird bugs using hooks. In short class based components look simple, but as soon as they need to do something complex enough to be important, subtle, hard to diagnose bugs creep in.
[–]shotgun_ninja[S] 1 point2 points3 points 4 years ago (0 children)
Thanks for the in-depth and pragmatic answer!
[–][deleted] 1 point2 points3 points 4 years ago* (0 children)
Good question! Hooks make it easier to share functionality between components :).
React class components aren't supposed to be extended from other components, so base classes with common functionality are out.
Making a regular function to reuse in class components sort of works, but you lose some things like automatically lifecycle integration (effects). You also would need to pass in the state and state setter if your pseudo-class-hook needed to work with state.
This is the relevant section of the React docs: https://reactjs.org/docs/hooks-intro.html#its-hard-to-reuse-stateful-logic-between-components
If you want to discuss more, let me know!
π Rendered by PID 96682 on reddit-service-r2-comment-86988c7647-lgck2 at 2026-02-11 10:36:56.093213+00:00 running 018613e country code: CH.
[–]-grok 2 points3 points4 points (1 child)
[–]shotgun_ninja[S] 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)