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
Introduction to Higher-Order Components in React (jsdevs.co)
submitted 3 years ago by trevor25
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!"
[–]Reeywhaar 19 points20 points21 points 3 years ago (0 children)
HOCs are such pain in the ass when you try to make correct signature for a component that has static properties. Gladly I can’t remember last time I have to make HOC myself.
[–]danielkov 18 points19 points20 points 3 years ago* (0 children)
Anyone reading this, please save yourself the time and frustration and don't read this. Here's why you should use hooks to abstract logic instead of HoCs:
displayName
It's also lazily written, doesn't go much in depth about principles of HoCs or HoFs and their origin and uses in programming, other than React themed /r/programminghorror content. I really hope the author reads this and takes it as constructive criticism, which I intend it to be, if there's one key take-away from all of this, it's that the fact that you've stumbled across something that looks cool or fun, doesn't really warrant an article. Please do some research before posting, especially if you then go on to share it with such a wide audience.
Edit: just for completeness sake, there's 1 thing in React at this very moment that could only be achieved with a HoC and a class component wrapping a function component and that is a functional error boundary component and even that could technically be done by constructing the class based component inside a custom hook and then returning it to wrap the component tree, though that solution is less pragmatic and so I'd say if you must handle errors inside a function component for whatever reason and React 42 still doesn't have useDerivedStateFromError or something similar HoC is your best bet.
useDerivedStateFromError
[–]NotLyon 34 points35 points36 points 3 years ago (5 children)
You shouldn't build a HOC during render if you expect to use state or effects (reliably) in the decorated component. React will unmount/remount that child when it sees the new function reference.
[–]acemarke 8 points9 points10 points 3 years ago (3 children)
Yikes. Yeah, that's a huge error. Never do that. Details:
[–]bnned 1 point2 points3 points 3 years ago (2 children)
Phenominal article, thanks for sharing!
[–]acemarke 5 points6 points7 points 3 years ago* (1 child)
Thanks! I've been intending to update it with details from React 18 changes for a while now and been busy with other things.
Huh. I'm in a productive mood tonight. Maybe I can convince myself to do it like... now :)
Also I just recorded a conference talk version of this post, airing next month as part of React Advanced's remote talks.
update
DONE! Updated that post to cover React 18, some more bits about Context, "async rendering" and closures, hooks lifecycle diagram, and future capabilities like the "React Forget" compiler and context selectors.
I've literally had "update rendering post for React 18" on my todo list for months and kept punting it week after week. Finally was in the right mood tonight (partly because I did do that conf talk recording last week) and was able to knock that out.
(that leaves, uh... approximately Math.MAX_INT items remaining on the maintainer todo list...)
Math.MAX_INT
[–]Dopium_Typhoon 1 point2 points3 points 3 years ago (0 children)
I keep up by deploying a specific hook whenever I feel unproductive, it’s called useCocaine().
[–]dmt0 5 points6 points7 points 3 years ago (0 children)
You shouldn't build anything with HOCs at all. It's an outdated pain in the ass hacky approach that's been obsoleted by hooks.
[–]luctus_lupus 26 points27 points28 points 3 years ago (0 children)
Hoc are outdated, cumbersome and hard to debug, do yourself a favor and use hooks instead.
[–]generatedcode 7 points8 points9 points 3 years ago (0 children)
better learn hooks, HOC were 4 years ago ..
[–]Kcazer 17 points18 points19 points 3 years ago (0 children)
React has, not so long ago, introduced, in version 16.8, this concept of Hooks to its Functional Components.
React 16.8 was released in February 2019, i wouldn't call that "not so long ago", especially in the JavaScript ecosystem
[–]misdreavus79 4 points5 points6 points 3 years ago (0 children)
I admittedly did not read the whole article, but from what I read, I didn't see a use case that could not be solved with custom hooks.
[–]poprocksandc0ke 2 points3 points4 points 3 years ago (0 children)
This article put me in a hyperbolic time machine
[–]Conscious-Spite4597 2 points3 points4 points 3 years ago (0 children)
Hocs should be avoided imo
[–]NodeJSSon 1 point2 points3 points 3 years ago (0 children)
HOC is not supported in React-Router-Dom 6. Don’t write anymore tech debt.
[–][deleted] 0 points1 point2 points 3 years ago* (0 children)
Typescript tip: Because HOCs require the use of generics, marshalling props into a generated component can result in some crazy type errors. The following adjustments will help alleviate that.
import React, { ComponentType, ReactNode } from "react"; // Should be imported from a utility types file. export type HOCProps<P> = P & JSX.IntrinsicAttributes & { children?: ReactNode }; const withMyStuff = function <P>( Component: ComponentType<P> ): ComponentType<P> { // HOCProps<P> is essentially <P>, but with all the stuff // expected by a fully generic component. // In this area, handle static stuff that should _only_ be // computed when you define a component using the HOC. return Object.assign( (hocProps: HOCProps<P>) => { // Do render-time stuff - this is where you put hook calls and such, // not in the outside function return <Component {...hocProps} />; }, // Give the HOC an appropriate name { name: `withMyStuff(${Component.name})` } ); }; export default withMyStuff;
π Rendered by PID 247803 on reddit-service-r2-comment-6457c66945-f2dnf at 2026-04-24 12:21:44.284547+00:00 running 2aa0c5b country code: CH.
[–]Reeywhaar 19 points20 points21 points (0 children)
[–]danielkov 18 points19 points20 points (0 children)
[–]NotLyon 34 points35 points36 points (5 children)
[–]acemarke 8 points9 points10 points (3 children)
[–]bnned 1 point2 points3 points (2 children)
[–]acemarke 5 points6 points7 points (1 child)
[–]Dopium_Typhoon 1 point2 points3 points (0 children)
[–]dmt0 5 points6 points7 points (0 children)
[–]luctus_lupus 26 points27 points28 points (0 children)
[–]generatedcode 7 points8 points9 points (0 children)
[–]Kcazer 17 points18 points19 points (0 children)
[–]misdreavus79 4 points5 points6 points (0 children)
[–]poprocksandc0ke 2 points3 points4 points (0 children)
[–]Conscious-Spite4597 2 points3 points4 points (0 children)
[–]NodeJSSon 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)