Hacker News new | ask | show | jobs
by anty 2582 days ago
As someone learning React atm: Why is this developed as a hook, and not as a component?
4 comments

It is using a Component; it's subtle but in the example "Drawing" is a Function Component that is declared using an arrow function. Hooks don't replace Components, but are a way to have state in what were previously known as Stateless Somponents (now Function Components), as well as encapsulating other React features. I personally prefer good old Pure Components.
Most people do open source because they learn something new, and since hooks are new to React they chose to build it with hooks.
Seems like it does the drawing outside of React.

It takes a ref to a React rendered DOM element and then does its thing.

Yes, but you could do that in a component as well?
Sure, but I guess it's more flexible that way?
Okay, I guess you could attach that functionality to an existing component. It just feels wrong to me, that you would put something that looks like a separate component into a hook to inject into another component. I thought there was some benefit in doing it like this, that I cannot think of...
The main goal of React hooks was the shrinking of the component tree. Most people would probably create their own version of an SVG component and then they would have to nest the library component. Now they just have to throw in the hook and be done with it.
I think that this one is flexible and easy to adapt to the UI.