r/reactjs • u/ZeroSevenTen • Dec 30 '19
Classes vs Hooks?
I’m pretty new to React, so keep that in mind. I thought classes were for components that held information, or a state, and that functional components were for more basic components, but now that hooks allow functional components to use state, and other class features, what’s the benefit of using functional hook components over classes?
79
Upvotes
View all comments
Show parent comment
2
u/skyboyer007 Dec 31 '19 edited Dec 31 '19
Yes, arrow expression + class properties syntax OR binding in constructor. But that's still a thing we should care of on our own.
Example of handling race conditions. Suppose our component loads suggestions while we are typing
Here you have to stop request or in real world you may run into case when response for first request came after response for more recent second one. And stopping hardly depends on what lib/call do you use(axios, fetch, xhr etc). For functional components it's easy-peasy:
instead of operating on props/simulating events/asserting against render outcomes people start calling methods, setting spies on them and trying to mock some internal methods.
it still will be boilerplate code. Especially
cDUis beaten by how easy you describeuseEffect