News for hackers. Stuff that matters.
Re:React Docs Beta – rewritten with hooks and interactive ex... by acemarke on Saturday October 23, 2021 @12:31PM (#28969981)
I am _so_ very excited about this! This is going to be a huge help for the community. These new docs are an incredible improvement over the old docs. In-depth tutorial and concept explanations, live editable sandboxes, coverage of key concepts and common gotchas, and much more!

See the docs repo PR [0] for background on what content is currently included, plans for further content and site improvements, and feedback on the new docs.

For why this is a big deal: the old docs were good in some ways, but very weak in others.

By far the biggest issue is that hooks were announced in Oct 2018 and released live in early 2019, but the tutorials and explanations were all still written with class components and showed older patterns, well after the community had fully adopted hooks as the standard approach. The hooks info was a totally separate section of the docs. That section was _good_, but it made it really hard to continue recommending the React docs as a good starting point for someone new to React.

The docs have also skimmed past a lot of important information, usage concepts, and info on how React itself actually works. For example:

- React's batching behavior is something that frequently trips people up, but the current docs just have a note that says "updates may be async"

- Using the hook dependency arrays correctly is vital, but the current hook docs describe it as sort of an after-thought

- There's no information on what a "reducer function" is or how to use one correctly.

That's part of why I ended up writing a 9K-word "Guide to React Rendering Behavior" post [1], and Dan wrote a similar "Guide to `useEffect`" [2] article.

Fortunately, the new docs address a lot of those concepts directly, which should really help people who are getting started.

[0] https://github.com/reactjs/reactjs.org/pull/3965

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

[2] https://overreacted.io/a-complete-guide-to-useeffect/

Re:React Docs Beta – rewritten with hooks and interactive ex... by tbezman on Saturday October 23, 2021 @02:35PM (#28971071)
Potentially controversial post on why we memo all the things. https://attardi.org/why-we-memo-all-the-things/
Re:React Docs Beta – rewritten with hooks and interactive ex... by lewisl9029 on Saturday October 23, 2021 @05:53PM (#28972714)
I also generally fall on the side of useMemo'ing everything pre-emptively, and this is a good argument for its effects on performance. However I think a more important effect is that preserving references between renders (and lack thereof) now has a direct effect on behavior due to useEffect's dependency list.

If a downstream component's useEffect depends on a prop that hasn't been useMemo'd, their effect will be executed on every render due to the changing reference, and there is nothing that can be done at the downstream component to alter that behavior. This means they'd have to trace the prop back to its source and refactor it to useMemo, which can be a very painful exercise as I'm sure anyone who has done it can attest to. For props that come from third party libraries, this might not even be an option, which is why pre-emptively useMemo'ing is even more crucial for reusable abstractions like shared hooks and components.

And yes, I realize that the React docs currently recommend using useMemo only as a performance optimization, not as a semantic guarantee, but I believe that ship has sailed. There is so much code in the wild today that useMemo and then pass the result to some useEffect downstream, that they can't really afford to break in practice. I think the only practical option at this point is to strengthen the original useMemo with a semantic guarantee, and then introduce a separate useMemoForPerformanceOnly (with a better name) hook that can be used to opt-out of the semantic guarantee to allow React to evict memoized results to optimize for memory usage.

Re:React Docs Beta – rewritten with hooks and interactive ex... by mattwad on Saturday October 23, 2021 @03:30PM (#28971472)
I feel like you could go more into depth to defend your assertion "no memo is not an option." As someone who has spent hours on jsperf back in the day -- my apps feel blazing fast already. Many devs I know also don't use memo or useCallback at all. It sounds like you're adding an entire layer of extra abstraction "just in case." But then again, maybe your use case makes it warranted.
Re:React Docs Beta – rewritten with hooks and interactive ex... by eatonphil on Saturday October 23, 2021 @05:36PM (#28972564)
Same, I haven't seen a need for it for performance yet across a few companies and open source apps I've worked on.
Re:React Docs Beta – rewritten with hooks and interactive ex... by eh9 on Sunday October 24, 2021 @12:23AM (#28974950)
Every single time I’ve needed to use memo the performance gains where obvious.
Re:React Docs Beta – rewritten with hooks and interactive ex... by sibeliuss on Sunday October 24, 2021 @01:14AM (#28975174)
A: You'll know when you need it. Until that point one is firmly in the realm of premature optimization.
Re:React Docs Beta – rewritten with hooks and interactive ex... by atom_arranger on Sunday October 24, 2021 @07:51AM (#28977071)
The post points out that given memoization in many cases avoids serious perf/functional issues not memoizing may be considered premature optimization.
Re:React Docs Beta – rewritten with hooks and interactive ex... by aszen on Saturday October 23, 2021 @03:07PM (#28971299)
Interesting post, makes u wonder why the hell doesn't react do it by default. I'm really surprised to see that i have to think about performance in react apps so much, coming from elm i expected it to be all faster by default
Re:React Docs Beta – rewritten with hooks and interactive ex... by brundolf on Saturday October 23, 2021 @05:04PM (#28972254)
Because Elm has language-level support, while hooks try to do things that should be language-level but aren't, which is why they have so many footguns in general.

To get concrete, there's no possible way this function could be automatically memoized at a library/framework level:

  const myCallback = () => setFoo("bar")
Re:React Docs Beta – rewritten with hooks and interactive ex... by dham on Saturday October 23, 2021 @04:24PM (#28971898)
I wonder every day why React doesn't do a lot of things by default. Imagine specifying memoization dependencies by hand. Most of the stuff is named or put in the framework to raise engineering salaries for making CRUD applications or to make them feel smarter about doing basic web work.

React will go the way of JQuery in 5 years. We'll kindly thank it for it's contribution to the framework space and move on. No one in their right mind would pick it up for new apps over Vue, Svelte, Angular hell even Ember. Then you have the new wave of frameworks coming out. Only way that React is usable is only for the view layer with Mobx holding any other state.

Re:React Docs Beta – rewritten with hooks and interactive ex... by catlifeonmars on Saturday October 23, 2021 @03:22PM (#28971399)
Makes sense. How about making that the default behavior of React and providing a React.dontMemo(…) API to opt out?
Re:React Docs Beta – rewritten with hooks and interactive ex... by midrus on Sunday October 24, 2021 @01:48PM (#28980244)
Or even better, provide a top level configuration option, like:

ReactDOM.render(element, document.getElementById('root'), {memo: true});

Just dreaming...

Re:React Docs Beta – rewritten with hooks and interactive ex... by catlifeonmars on Monday October 25, 2021 @11:07AM (#28988643)
This one is interesting, because React has a separation between the renderer and the library. There is a different renderer for e.g. react native, or even for the terminal (!!). It’s a little ambiguous where memoization falls (does it belong to the renderer, or to the core?). I totally agree that being able to switch on behavior for an entire tree or subtree would be ideal, but react tends to use components for that as well (e.g. <React.StrictMode>). Perhaps this should be a component, it could even be implemented via the context system.
Re:React Docs Beta – rewritten with hooks and interactive ex... by midrus on Sunday October 24, 2021 @01:43PM (#28980219)
This is great. And great explanation of the reasonings.

I wonder why React doesn't provide a configuration/flag/option to just do this by default if anybody wants to.

Re:React Docs Beta – rewritten with hooks and interactive ex... by cageface on Sunday October 24, 2021 @02:38AM (#28975450)
The problem with using useMemo & useCallback everywhere is that the dependency arrays are really easy to get wrong. Small changes can easily lead to stale data or render loops. I'd much rather people take the time to consider each case. The React profiler is your friend here.
Re:React Docs Beta – rewritten with hooks and interactive ex... by kylorhall on Sunday October 24, 2021 @04:31AM (#28975924)
I think if you use them 100% of the time alongside eslint dependency arrays are really hard to get wrong. If you rarely use them, you always get them wrong.
Re:React Docs Beta – rewritten with hooks and interactive ex... by cageface on Sunday October 24, 2021 @04:37AM (#28975968)
If you just blindly follow the eslint suggestions you can very easily wind up with render loops. You need to carefully consider and understand the dependencies in each case. And if you want to do something specific when only one of your dependencies changes you need even more gymnastics.
Re:React Docs Beta – rewritten with hooks and interactive ex... by atom_arranger on Sunday October 24, 2021 @07:54AM (#28977084)
The reason you end up with render loops is usually because you didn’t memo something. If you memo everything you can avoid that issue.
Re:React Docs Beta – rewritten with hooks and interactive ex... by cageface on Sunday October 24, 2021 @02:36AM (#28975445)
All the elegance of React & hooks falls apart once you have to start worrying about performance. Managing dependency arrays is tedious and error prone and the source of many subtle bugs. Code stuffed full of memo calls is not pleasant to read. It really feels like you're doing the work that a language or complier should be doing for you.
Re:React Docs Beta – rewritten with hooks and interactive ex... by seedboot on Saturday October 23, 2021 @01:12PM (#28970362)
Congratulations I've been looking forward to starting reading this for some time!
Re:React Docs Beta – rewritten with hooks and interactive ex... by acemarke on Saturday October 23, 2021 @01:16PM (#28970391)
heh, to be clear, _I_ didn't work on this at all :) It was primarily Rachel Nabors and Dan Abramov from the React team doing the writing:

https://beta.reactjs.org/community/acknowledgements#react-do...

(I did get to see an early alpha preview version and gave some feedback - other than that I've just been very eagerly awaiting this coming out)