r/javascript • sand was never meant to think • Sep 10 '24

Use react hooks in classes

https://github.com/NullVoxPopuli/classy-react/tree/main
3 Upvotes

12 comments sorted by

View all comments

11

u/markus_obsidian Sep 10 '24

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.

1

u/nullvoxpopuli sand was never meant to think Sep 10 '24

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

1

u/markus_obsidian Sep 10 '24

I usually need an existing, third-party hook that pulls in state from some external source--redux, swr, react-query, etc.

1

u/nullvoxpopuli sand was never meant to think Sep 10 '24

Do all those return a tuple of get/set? Or do they return a variety of things?

4

u/markus_obsidian Sep 10 '24

No consistency whatsoever

1

u/nullvoxpopuli sand was never meant to think Sep 10 '24

I think we can make something work -- worst case scenario, we special-case certain hooks