News for hackers. Stuff that matters.
Re:A Critique of React Hooks by sibeliuss on Monday April 27, 2020 @11:40AM (#22996866)
It's unreal that in React I have to deal with occasional infinite loops now because of hooks. Sure, React catches the loop cycle so things don't totally freeze but I don't recall ever having to deal with this before them. Weird, unexpected reference issues, missing dependency accidents requiring linters to prevent, strange programming patterns, a team member having to write a terrifying novel like https://overreacted.io/a-complete-guide-to-useeffect/ for something that was never really a problem before. The list goes on and on.
Re:A Critique of React Hooks by s-km on Monday April 27, 2020 @12:44PM (#22997482)
>It's unreal that in React I have to deal with occasional infinite loops now because of hooks. Sure, React catches the loop cycle so things don't totally freeze but I don't recall ever having to deal with this before them ... missing dependency accidents requiring linters to prevent

Infinite loops and missing dependencies are/were issues with `componentDidUpdate`/`componentWillUpdate` and `componentDidMount` as well, though. On the plus side, you now have a linter which can both point out these errors and automatically fix them for you. I agree that the whole thing is a bit leaky and dumb though, but there's no way to fix that without introducing some sort of compilation/optimization step and afaik the React guys aren't really considering that at the moment.

>Weird, unexpected reference issues

Not sure I've run into this before. Do you have any examples?

>strange programming patterns, a team member having to write a terrifying novel

The first bit seems like personal preference or something, not sure what you're referring to as strange. The `useEffect` novel exists because a ton of people had built up their (incorrect) mental model of how React works with lifecycle methods and were making mistakes or running into unexpected behaviour because they assumed `useEffect` was the exact same thing as `componentDidMount`.

Re:A Critique of React Hooks by sibeliuss on Monday April 27, 2020 @01:35PM (#22997980)
Reference issues:

1) Needing an advanced understanding of closures. Not always, but sometimes. That "sometimes" is often unintuitive, requiring weird solutions like useRef. Good luck beginners.

2) Things like updating reducer state by using a spread object, which creates a new object which can then send a system haywire. Seems fine, and is mostly fine in most cases, but hey, oftentimes not fine, and why that's so is not always clear. So then there's memoization, and useCallback and all of these safety checks -- each with their own dependencies array that need to be checked. It's really too much tbh. There are lots of solutions out there that use proxies to check this stuff; React should have baked that into the core and completely removed that responsibility from the user unless they wanted to opt-in micromanage performance of their code.

Re:A Critique of React Hooks by ng12 on Monday April 27, 2020 @03:08PM (#22998874)
> Things like updating reducer state by using a spread object, which creates a new object which can then send a system haywire

This is what you should be doing and not doing this seems more likely to cause problems. Do you have an example?

Re:A Critique of React Hooks by exogen on Monday April 27, 2020 @12:19PM (#22997247)
The problem before was that your class component was not updating correctly and rendering stale & out of sync data. If it were updating correctly, it would have had the same infinite loop problems.
Re:A Critique of React Hooks by ng12 on Monday April 27, 2020 @12:33PM (#22997378)
Exactly this. Class components let you cheat and easily write components that were broken if a prop was unexpectedly updated. Hooks will surface this immediately.
Re:A Critique of React Hooks by dgritsko on Monday April 27, 2020 @12:32PM (#22997364)
You could easily wind up with an infinite loop without hooks (for example, by calling `setState` in `componentDidUpdate`).
Re:A Critique of React Hooks by jiofih on Monday April 27, 2020 @12:56PM (#22997594)
That would be trivial to spot. Not the case with hooks.
Re:A Critique of React Hooks by torartc on Monday April 27, 2020 @08:04PM (#23001758)
It's trivial to spot with hooks.
Re:A Critique of React Hooks by charrondev on Monday April 27, 2020 @10:04PM (#23002572)
This is definitely not the case. I’ve seen enough class components with complicated componentDidUpdate methods. The method would then be broken up into multiple other smaller methods (or to re-use some logic in didMount and didUpdate)

A few iterations later and you have setState peppered throughout your component.

More often then not though I’ve seen a lot of class components that would just fail to update when certain props change. It’s much harder to miss these cases with hooks.