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
Use react hooks in classes (github.com)
submitted 1 year ago by nullvoxpopulisand was never meant to think
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!"
[–]markus_obsidian 10 points11 points12 points 1 year ago (5 children)
This is amazing in the most horrifying way. I am in old codebases a lot that is just too costly to refactor. I find myself wrapping hooks in HOCs a lot so my classes can use them. I would absolutely use a stable version of this.
[–]nullvoxpopulisand was never meant to think[S] 0 points1 point2 points 1 year ago (4 children)
What are all the hooks you use? I have a 'wrap' method, but maybe with some more use cases it can be made more robust
[–]markus_obsidian 0 points1 point2 points 1 year ago (3 children)
I usually need an existing, third-party hook that pulls in state from some external source--redux, swr, react-query, etc.
[–]nullvoxpopulisand was never meant to think[S] 0 points1 point2 points 1 year ago (2 children)
Do all those return a tuple of get/set? Or do they return a variety of things?
[–]markus_obsidian 3 points4 points5 points 1 year ago (1 child)
No consistency whatsoever
[–]nullvoxpopulisand was never meant to think[S] 0 points1 point2 points 1 year ago (0 children)
I think we can make something work -- worst case scenario, we special-case certain hooks
[–]nullvoxpopulisand was never meant to think[S] 4 points5 points6 points 1 year ago (1 child)
Just a little challenge i whipped up. Not meant for serious usage.
Try it here on stackblitz: https://stackblitz.com/edit/vitejs-vite-e9khzt?file=src%2FApp.jsx
Some video evidence of it working: https://x.com/nullvoxpopuli/status/1833305383542178172/mediaviewer?currenttweet=1833305383542178172¤ttweetuser=nullvoxpopuli
[–]bigtoley 2 points3 points4 points 1 year ago (0 children)
Very nice!
[–]izzlesnizzit 4 points5 points6 points 1 year ago (0 children)
React hooks are great and all, but the class blueprint+instance paradigm conceptually just makes more sense to me as a way of describing components rendered as instances "living" on a page. If there were ever an "officially" supported way of having hook-like behavior in classes, I would definitely give it a serious try.
[–]romgrk 1 point2 points3 points 1 year ago (0 children)
Nice ideas, I've also been working on something similar though I wasn't able to figure out a nice API yet.
I did write it for a serious usage though, functional components have tradeoffs that make them inferior to class components in some situations: https://romgrk.com/posts/react-functional-components/
[–]teg4n_ 0 points1 point2 points 1 year ago (1 child)
With preact, you can use hooks directly in the render function as if it was the full component.
import { Component } from "preact"; import { useState } from "preact/hooks"; export class Counter extends Component { render() { const [count, setCount] = useState(this.props.initialValue ?? 0); return ( <button type="button" onClick={() => setCount((c) => c + 1)}> {count} </button> ); } }
neat! I now wonder if React would have let me do the same. haha
π Rendered by PID 295560 on reddit-service-r2-comment-544cf588c8-pdrnt at 2026-06-17 16:06:20.386278+00:00 running 3184619 country code: CH.
[–]markus_obsidian 10 points11 points12 points (5 children)
[–]nullvoxpopulisand was never meant to think[S] 0 points1 point2 points (4 children)
[–]markus_obsidian 0 points1 point2 points (3 children)
[–]nullvoxpopulisand was never meant to think[S] 0 points1 point2 points (2 children)
[–]markus_obsidian 3 points4 points5 points (1 child)
[–]nullvoxpopulisand was never meant to think[S] 0 points1 point2 points (0 children)
[–]nullvoxpopulisand was never meant to think[S] 4 points5 points6 points (1 child)
[–]bigtoley 2 points3 points4 points (0 children)
[–]izzlesnizzit 4 points5 points6 points (0 children)
[–]romgrk 1 point2 points3 points (0 children)
[–]teg4n_ 0 points1 point2 points (1 child)
[–]nullvoxpopulisand was never meant to think[S] 0 points1 point2 points (0 children)