News for hackers. Stuff that matters.
Re:Common mistakes writing React components with hooks by steinuil on Tuesday July 21, 2020 @10:07AM (#23906873)
The first one just feels like a premature optimization. Yes calling setCount forces a rerender of that component, but unless there's lots of subcomponents inside that component, I wouldn't bother. Chances are later you'll need that state in the view, and if you have "unexpected side effects" from rerendering then that is the problem.

The other tips are fine; effects should have a single responsibility and links and buttons should be accessible.

Re:Common mistakes writing React components with hooks by brlewis on Tuesday July 21, 2020 @11:26AM (#23907557)
Yes. The author should replace "This is dangerous" with "This is a tiny bit sub-optimal" in the first example.

EDIT: "This is dangerous" is also the wrong label for the 2nd example. Should be "This is not the cleanest way"

EDIT2: "This is dangerous" is also the wrong label for the 3rd example. Should be "This is not the most readable". I'd also note that I'm surprised the author has seen this mistake a lot; the "solution" looks like the happy path most people would follow in the first place.

EDIT3: "This is dangerous" in the 4th example should be replaced with "This could be simplified"

EDIT4: "This is dangerous" in the 5th example should be "This makes redundant API calls". I'd also note that I'm surprised this example is even included. How could anyone miss that fetchData() is being called every time updateBreadcrumbs() is called?

Re:Common mistakes writing React components with hooks by vvpan on Tuesday July 21, 2020 @11:54AM (#23907867)
Yeah, there are gotchas and issues with hooks and some of them are quiet common but this article does not cover those. Odd selection of "mistakes".
Re:Common mistakes writing React components with hooks by jgwil2 on Tuesday July 21, 2020 @05:34PM (#23911251)
Well, example 2 is legitimately dangerous if your app is required to be accessible.
Re:Common mistakes writing React components with hooks by city41 on Tuesday July 21, 2020 @10:22AM (#23906990)
To add to this, the React docs state you should never rely on renders for side effects. In dev mode, React will even render your components twice to suss out any render relying side effect bugs. Your component should always work the same whether it's rendered once or rendered a million times.
Re:Common mistakes writing React components with hooks by duxup on Tuesday July 21, 2020 @10:12AM (#23906909)
Yeah I felt the same way about the first one.

Yeah we're preventing re-renders there but what are we really preventing / saving, nothing significant?

Anytime I run into a situation where I didn't like how / when a component was re-rendering, it absolutely was not because there was a random bit of state like a counter was in state that didn't need to be... usually it was just a more complex situation unfolding.

It's a good illustrative example, but not a 'common mistake' IMO.

Re:Common mistakes writing React components with hooks by bulgr0z on Tuesday July 21, 2020 @10:36AM (#23907089)
> The first one just feels like a premature optimization

It is in a browser context, but less with React Native which relies way more on refs. It also never hurts (imho) to explain why refs exist and why/where to use them as they can easily be abused (and often are) by devs trying to replicate OOP patterns in React.

Re:Common mistakes writing React components with hooks by Kiro on Tuesday July 21, 2020 @12:53PM (#23908482)
I have a really large chat app with thousands of users and I realized the other day that I accidentally re-render the root component on _every keystroke_. Still, it's not a disaster and I haven't bothered to fix it because no-one notices.
Re:Common mistakes writing React components with hooks by steinuil on Tuesday July 21, 2020 @03:18PM (#23909958)
Shouldn't be a big deal if it's properly split into subcomponents and their props don't change with every rerender as well.