|
|
|
|
|
by eyelidlessness
2017 days ago
|
|
Well if I wasn’t already convinced that hooks were a bad idea, this cemented it for me. What a bunch of confusing complexity to mix up with something that otherwise looks like a function. Data in, data out, and mysterious side effects that look like functions too! Track them... with dev tools I guess? I’m sure they’re plenty testable... with mocks? I’ve spent a lot of time criticizing angular for implicit magic, but at least they’ve tried to make it seem like you can pass values and substitute things. I still vastly prefer JSX for frontend dev but I think really that’s about all I care for in this ecosystem. |
|
useRef is a hook to serve just that purpose as is useState and useEffect.
Building new hooks off of these base hooks lets you write your own custom hooks to handle whatever side effects you need.
The existence of a hook is what should alert you to the fact that there may be some side effect thing happening here.
Not sure if this applies to you but I've found that most people who criticize hooks haven't made the leap to using custom hooks and have instead only used ones provided to them (useState, useEffect, etc).
Hooks are an incredible way of encapsulating behavior, separating out that behavior from a component, and exposing that behavior to any component that needs the same behavior. Check out this video for a practical example: https://www.youtube.com/watch?v=nUzLlHFVXx0
I'm in the middle of a big refactor and writing reusable custom hooks are an absolute savior to share functionality across components that may have nothing to do with each other.
Hooks are functions, which is why we can compose them to build new behavior from smaller ones. That's the point. Just cause a thing is a function doesn't mean its pure. Like I said above, hooks are often meant to contain the impure parts of your otherwise declartive and pure UI expressing the UI as a function of state.