Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Arnaud Ambroselli
Arnaud Ambroselli

Posted on • Edited on

     

I don't like React Hooks

Let's go strait to the point : I don't like Hooks because they are not intuitive to me.

Classes areso intuitive that I love them.

class App extends React.Component {  state = {  }  componentDidMount() {}  componentDidUpdate(prevProps, prevState) {}  componentWillUnmount()  render(){    return()  }}
Enter fullscreen modeExit fullscreen mode

It's almost if there is a README to understand what's going on there.
I became a developer at 30, two and a half years ago, and I started directly by learning#JavaScript30 by Wes Bos, thenReact for Beginners. I knew nothing about programming : classes, whatever... but React was so straightforward to embrace that it was a pleasure.

I can't say the same with hooks.

const App = () => {  const [someState, setSomeState] = useState(initState)  useEffect(() => {    return () => {}  }, [])}
Enter fullscreen modeExit fullscreen mode

OK, much less code I have to admit.
useState is easy to get, I also have to admit. For a simple functional component which needs a state, this is great. But for more complex component, actually I would say as soon as an equivalent ofcomponentDidMount is needed, I much prefer classes.

After two projects and two months full time on hooks, I am still not confident how to clear a timeout, how to use an effect only on mount. I start to be confident on fetching async data in an effect, but that's it. OK, refs are quite straight forward, I also have no problem with them.
But what about theuseCallback ? Sometimes, quite often, but not always, when I call a callback in my effect, I am forced by my linter to useuseCallback, and I couldn't tell you why some times yes and why some other times no. Maybe I need a few more weeks to be fluent in Hooks, but as I don't like them it might be more...

I am working for the first time with a friend, on a hooks project for the last two months. My friend is chasing the code lines, to reduce them at max, and he is also chasing code readability. He is quite an experienced programmer, so I learn quite some stuff from him. He doesn't like redux, so I had to think without it, it was great. And he loves hooks, so I had to deal with them, no choice.

And I'll remember two philosophical principles out of my bad experience with Hooks :

  • if a piece of code with less lines is less understandable than a piece of code with more lines, use it with more lines
  • a good API is an API where the README is as small as possible, and the number of times the need of going back to the README is as little as possible.

Sorry Dan, but from now on, I'll keep hooks for stateful simple functional components, like forms. But whenever I can, I will use classes.

Top comments(4)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
webketje profile image
webketje
Webdev/analyst based in Belgium
  • Location
    Belgium
  • Work
    IT Consultant at AUSY
  • Joined

This:

if a piece of code with less lines is less understandable than a piece of code with more lines, use it with more lines

= GOLD

CollapseExpand
 
pensarfeo profile image
Pensarfeo
  • Joined

Neither do I! My disappointment with hooks lead me to write a small HOC that allows writing stateful components without functions, and with all the functionalities of classes.

github.com/Pensarfeo/react-makesta...

This is highly experimental, so I would appreciate some feedback on the project!

PS: No hooks were harm wile writing this package :)

CollapseExpand
 
niceguynimni profile image
Guy Nimni
  • Joined

Well, thank you for saying out load what I have felt for a long time now.

CollapseExpand
 
aressler38 profile image
Alexander Ressler
  • Joined

Agreed! There is a time and a place for functional programming paradigms, but I don't think it belongs in a classical component based architecture.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More fromArnaud Ambroselli

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp