News for hackers. Stuff that matters.
Re:A Visual Guide to UseEffect by jasonkillian on Monday December 26, 2022 @09:10PM (#34144202)
Agreed, but it's a pretty big footgun because `useEffect` is often the seemingly easiest way to do the things you list.

I've had similar conversations many times with coworkers before when they were using `useEffect` to keep a state value in sync with a prop. The officially-recommended alternative of manually storing and updating an extra piece of state containing the previous prop value is cumbersome and also had ways it can go wrong. So, since `useEffect` works well enough in most cases and is easier, often the code just sticks with that method. I'm not entirely sure what's really best all tradeoffs considered, but it definitely illustrates how rough edges often pop up in hook-based React.

Re:A Visual Guide to UseEffect by nwienert on Monday December 26, 2022 @10:50PM (#34144878)
You don’t have to store the previous, you can just do an if != right in the render function and call setState there. That’s something I learned from the new beta docs.
Re:A Visual Guide to UseEffect by aidos on Tuesday December 27, 2022 @03:44AM (#34146712)
Can you point to that in the docs? Calling setState during render sounds like a fine way to get infinite loops.

Any of these sorts of hacks tend to be indicative of something not being structured poorly.

Re:A Visual Guide to UseEffect by mynameisvlad on Tuesday December 27, 2022 @04:22AM (#34146989)
Isn’t it also explicitly discouraged with a warning because of how bad it could be?

The React docs are very clear on this:

> The render() function should be pure, meaning that it does not modify component state, it returns the same result each time it’s invoked, and it does not directly interact with the browser.

https://reactjs.org/docs/react-component.html#render

Re:A Visual Guide to UseEffect by nwienert on Tuesday December 27, 2022 @04:50AM (#34147170)
Re:A Visual Guide to UseEffect by aidos on Tuesday December 27, 2022 @08:12AM (#34148296)
Interesting. Thanks for following up.
Re:A Visual Guide to UseEffect by mynameisvlad on Tuesday December 27, 2022 @11:12AM (#34149899)
That’s such a weird 180 on their part considering how explicit they have been about it.

I guess they did a bunch of work to make the case work and are confident about it, but it just seems like such a weird decision considering they’ve been adamantly telling their devs not to do this for years.

Re:A Visual Guide to UseEffect by acemarke on Tuesday December 27, 2022 @12:13PM (#34150815)
Calling `setState` while rendering has been a legitimate thing to do since hooks first came out, but _only_ in one specific scenario / usage pattern.

From the original hooks FAQ at time of release:

- https://reactjs.org/docs/hooks-faq.html#how-do-i-implement-g...

I talked about the reasoning / behavior for this in my React rendering guide:

- https://blog.isquaredsoftware.com/2020/05/blogged-answers-a-...

Re:A Visual Guide to UseEffect by mynameisvlad on Tuesday December 27, 2022 @12:28PM (#34151075)
That still doesn’t change my comment.

It used to explicitly be a thing you must not do for years, it’s weird to go straight 180 on something you actively warn developers not to do. If it wasn’t well documented in the past, then that’s reasonable, but actively spitting out console warnings and explicit documentation warning against it is just going to mean that anyone who isn’t new to react is going to be extremely weary about it.