r/reactjs • u/callensm • Oct 31 '18
Why the hate for React Hooks?
I've been seeing a lot of mixed feelings about React Hooks since it was announced and can't quite figure out why. Sure it's a deviation from the norm, but completely optional and certainly lowers the barrier to entry for new developers.
Can someone explain why all the negative feelings are rising about it?
19
Upvotes
19
u/joesb Oct 31 '18
“Completely optional” is not a valid argument when you realize people don’t code in vacuum. They’ll have to read other people’s code.
I’m not sure if “lower barrier” to new learner a good thing in this case. New learner people should be concerned is usually is a new learner of both the library, language, programming concept and practice. Now look at the way hook is called. It’s a global function that relies on order of the called it is made. Honestly, do you really want “new learner” to see this code as an example of how to write a function? Newbies don’t know what good code design is. They don’t know why global is bad. Now I’m gonna have them learn a library that relies on making global function calls to make state as an example?
11
u/Veranova Oct 31 '18
I love the idea of using a different approach than component hierarchies for injecting things, and making it clearer where things come from.
I dislike the idea of introducing extra complexity. Reading a custom hook is a confusing experience, and I won't expect many of my colleagues to understand them. HOCs also suffer from this though, and the way a hook is used is an improvement on that.
Classes as components (and life cycle methods) are great because every developer knows how to use them, but the functional/spaghetti approach I've seen for custom hooks creation is a step away from that. I'd rather see the API designed as a new Base class with life cycle methods and a
React.createHook(Class)method to create the hook.13
u/leixiaotie Oct 31 '18
For me, I'm more concerned rather than hate, and it's because how easy react hooks can be used, and how their side effect can pose potential problem which I can't identify now (but someone will in the future).
One of example, using a custom hook like this:
const count = useCustomHook(id);, what criteria and how many times it will cause re-render? A custom hook can manage it's own state and isn't restricted at how many it can have or even if it use another custom hook. A state change will do re-render, no matter where it come from. It'll somehow be mitigated by SUSPENSE, we'll see later how it'll handle them.Another concern, rather than keeping state and state change in respected place / state manager, now we can easily use another in smaller component. I imagine there will be more call to fetch master data / list data from hooks rather than from state manager / App's
componentDidMount. Keeping state ofwindow.widthfromuseWindowWidth, etc. In other side, it'll makes for easier tracking via googleTrack, so it'll be moreuseGoogleTrackuse in SPA.Another very minor concern, without react hook knowledge,
useStateseems live in global scope and singleton, similar with how redux work (which live in Provider). In other case, twouseStatecalls return two unique state manager, which lives in component. You won't get any info about that without reading documentation / trying it yourself.There are more which I can't describe, I just feel there may produce many issue (either intentionally - handled by react or not) with this approach.
6
u/drink_with_me_to_day Nov 01 '18
It'a mystical and magical. And worse yet it tells me how to program.
It's like I'm conjuring up some state out of nowhere, and I must follow the linting rules or else...
5
u/[deleted] Oct 31 '18 edited Oct 31 '18
I'm just not sure I understand the point. Every article I've read about it mostly focuses on how it's easier to learn than class components, but I never found class components particularly difficult to write, and anyway, I'm comfortable with them now. I barely write functional components at all anymore, because I usually end up converting them to classes down the road anyway.
12
u/natmaster Oct 31 '18
Not sure about hate, but I'm definitely skeptical because of it being less intuitive and confusing for code. Not sure why you would think expanding API surface area and having confusing things like restrictions on how you write a function are going to be more intuitive than a simple class with a few lifecycle methods.
I can definitely see some advantages like making hot reloading easier, optimization easier, unit testing easier, less components in the tree. However, they didn't stress these and instead said weird things like you that this is somehow easier to understand? I can see how putting behavioral wrappers in something called a component is a bit counter-intuitive as well, but the class model is much simpler to understand what's going on and less likely to make accidents happen.
11
u/Capaj Oct 31 '18
Do you have some links? I haven't seen a good example of hate toward it yet. Sure, there were couple of mean comments here, but they were downvoted heavily if I remember correctly.
4
u/smackfu Oct 31 '18
I don’t particularly like the design of the built-in hooks they have demonstrated. It feels like they shoved stuff wherever they could fit them in non-obvious places. For instance, no one is going to figure out what the second argument of the useEffect hook does without looking it up in the docs. It just feels like the amount of API a new developer will have to learn has increased for not much gain.
4
u/R3PTILIA Nov 05 '18
People resist change. Thats a fact of life. Hooks are an amazing addition. Separation of concern vs cycle of component improves readability 10-fold.
7
u/brianvaughn React core team Oct 31 '18
Why the NSFW tag? 🤔
Edit: Maybe this is just a mobile Reddit bug. A few of the self posts in /r/reactjs are tagged as NSFW for me.
9
u/ngly Oct 31 '18
Is it okay to say I don't like the API syntax? Something about a function that takes callbacks, different types, and has to return certain values to do specific things feels awkward to me. I prefer named methods that make it clear what's happening instead of relying in argument order and return values. Specifically referring to
useEffecthttps://reactjs.org/docs/hooks-reference.html#useeffect. And the hooks don't have the same arguments so you have to memorize what the ordering does for each Hook.13
u/theHorrible1 Oct 31 '18
I dont get the circle jerk on using functions all the time. I'd sooner ask why all the hate on classes when you want life cycle methods and locale state. I also dont like this line from the hooks faq " In the longer term, we expect Hooks to be the primary way people write React components." Why would I want to do this? IMHO classes are much more intuitive when you need state etc.
8
u/swyx Oct 31 '18
many developers dont like change. i wouldnt be that interested in explaining the opinions of others, as much as just deciding if i like the change on its own merits.
2
u/idutta2007 Feb 22 '19 edited Feb 23 '19
Developers do not want to overload themselves with concepts like render props, higher order components, reducers only to be replaced by another one called "hooks". We have to maintain code written by others that uses these features.
More terms and concepts like these are introduced and abandoned, more convoluted the framework becomes over time. Please save Reactjs from having the same fate as Angular. Angular has concepts like transclusion', injector, provider, service and what not.
1
u/DecentStay1066 Dec 07 '21
After I read almost hundreds of hook project, I can see that the lover of hooks never and even not able to create a thing that more complicate than a button or form field. That's why they are still saying, yes, hooks are easy to learn, why use classes. No hierarchy, no organization, just follow their concept of "functional" programming without any namespace / folder structure, just put something in the global for easier calling.
1
u/DecentStay1066 Dec 10 '21
To hook lovers,
if you really want to have functional programming?
Why don't you try declare a Static class for that?
Don't argue that class structure has to create component for every tasks, we just have to create components for those visible, just like your functions.
Is some noob ReactJS tutorials teaching the noobs that needs to create components for all the invisible controls?
I have been heard so many times saying that you need to create a component for sending request, whatever? Do they even know how to write JS?
0
u/[deleted] Feb 07 '19
[deleted] — view deleted comment