3 ms·
Josh's post is excellent! Related, a couple years back I wrote a post on the same topic: "A (Mostly) Complete Guide to React Rendering Behavior" [0]. It's lon
by acemarke 4y ago
Josh's post is excellent!
Related, a couple years back I wrote a post on the same topic: "A (Mostly) Complete Guide to React Rendering Behavior" [0]. It's longer and has more details, but fewer diagrams :) (Josh's ability to make interactive posts is amazing.)
I originally wrote my "Rendering Behavior" post specifically because the existing React docs didn't clearly spell out this kind of behavior, and I was _constantly_ seeing questions that showed people didn't understand these concepts well. For example, many folks assume that "React re-renders my component when its props change", when in fact the real answer is that "React re-renders recursively by default, _regardless_ of whether or not any props changed". There's also a lot of confusion over how Context ends up affecting renders, and I've seen folks end up in situations where setting state in an "context provider" parent component ends up rendering the _entire_ app - not because Context changed, but because they did a `setState()` and that's the natural behavior. So, I was trying to help clarify those sorts of nuances.
I can say that it's been one of the top couple posts I've written in terms of traffic and positive feedback (along with my "Redux vs Context differences" post).
The good news is that the new React beta docs [1] _do_ cover at least some of this rendering info, although it's more contextual in various points of the explanations and in less detail. I'm hopeful that after the main tutorial/API reference material is done and the docs are opened up for external contributions, that we can help add a couple pages that cover some of this rendering behavior info as well.
There's a few other good articles I've also seen covering this topic as well [2] [3] [4].
[0] https://blog.isquaredsoftware.com/2020/05/blogged-answers-a-mostly-complete-guide-to-react-rendering-behavior/ https://blog.isquaredsoftware.com/2020/05/blogged-answers-a-...
[1] https://beta.reactjs.org https://beta.reactjs.org
[2] https://www.zhenghao.io/posts/react-rerender https://www.zhenghao.io/posts/react-rerender
[3] https://alexsidorenko.com/blog/react-render-cheat-sheet/ https://alexsidorenko.com/blog/react-render-cheat-sheet/
[4] https://www.developerway.com/posts/react-re-renders-guide https://www.developerway.com/posts/react-re-renders-guide
- westoncb 4y agoI've been looking for something like your rendering behavior article :) One question though: is it pretty up to date with changes since 2020? (I think there were some things in React 18 that would affect rendering behavior—not sure though).
- acemarke 4y agoHeh, unfortunately "update my rendering post to cover React 18" has been on my todo list for _months_ now :) As in, I literally have a todo list entry that I keep bumping back until "Next Sunday", because other stuff is higher priority. (like, trying to get Redux Toolkit 1.9 wrapped up and shipped... and also playing as much golf as possible while the weather is decent :) ) The shortest answer is that it's still effectively the same - the one major change is that React 18 will now batch _all_ updates in _any_ given event loop tick, regardless of whether those were queued up inside a React event handler or not. There's a good discussion on this in the React 18 Working Group post on "Automatic Batching": https://github.com/reactwg/react-18/discussions/21 https://github.com/reactwg/react-18/discussions/21
- westoncb 4y agoAh okay—no problem. Actually the only concrete rendering-related thing I'd seen on React 18 was about the auto-batching, so I'm already clear on the differences there. So I'll still be giving the article a read—thanks for you work on it, and enjoy the golf lol.