Hacker News new | ask | show | jobs
by Apotheos 1114 days ago
What's the new way as of a few months ago? The docs way?
1 comments

They may be referring to React 18 + Strict Mode meaning all useEffects can be called as many times as React wants. People before were writing code where useEffect on [] worked like onMount and was only called once.
The entire hooks "thing" felt like a case of the library authors being too clever.

Having components with lifecycle methods called at known times by the framework is a perfectly straightforward mental model. It's worked just fine since the beginning of time. iOS still uses it.

I remember investing hours (and days and weeks) migrating old code to the "new way" or trying to get some simple thing "working with hooks." Other than being able to share in the authors' feeling of smugness over the unnecessarily-convoluted mental model when we finally got it all working, I genuinely can't recall any value we got out of the whole investment.

I agree. Hooks felt to me like an attempt to go all-in on functional programming when components are perhaps easier for many people to think about as classes due to the unavoidable need to have state in the component. It seemed like an attempt at purism that, instead of making things simpler, made them, at least cognitively, a bit more complicated due to the irreducible complexity of state.
My experience has been the opposite. With hooks, it's now possible to decompose a concern and modularize it in full. Having logic for dealing with a concern spread over willMount/didMount/shouldUpdate/willUnmount was a total mess. It's bad enough with one concern, but if a component has 3 or 4 concerns that span across the lifecycle, it quickly became unwieldy. And for all the complains about people using `useEffect` wrong, I saw just as many people using setState calls in lifecycle methods and crazy things happening in mapStateToProps of class components.

In the end, hooks have greatly simplified things for me.

It turns out much of s/w development is a cult (many cults) with no basis for their teachings.
> Having components with lifecycle methods called at known times by the framework is a perfectly straightforward mental model. It's worked just fine since the beginning of time. iOS still uses it.

Android, too.

What's so infuriating about that is the documentation basically told people, hey instead of doing this, use useEffect the way you were using these methods.

So are the users wrong, or did you screw up?

> So are the users wrong, or did you screw up?

When you have to rewrite the documentation to basically tell the world "you're holding it wrong" then it says a lot about the thought that has (not) gone into designing the feature.

And that's why I think I'm done with React for now. It just does not spark joy.

If you're ignoring dependencies that should be on a call to `useEffect`, you're just making room for bugs.

If you're not properly tearing down what you're doing in an effect, you're just making room for bugs.

These have always been true of useEffect.

Then how do I emulate onMount now?
You have to use a useRef then inside your useEffect of [] you check if it is false and it it is you set it to true then run your code.

If anyone reads this and I am wrong please comment, but from what I can see that's the only solution.