I Was Wrong About Nested React Components



I just thought nesting component definitions in React was pointless, turns out it’s a big problem for React when it comes to maintaining state or doing efficient updates. Don’t do it!

👉 Practical Module Federation Book: https://module-federation.myshopify.com/products/practical-module-federation
👉 No BS TS (The Book): https://no-bs-ts.myshopify.com/
👉 I’m a host on the React Round-Up podcast: https://devchat.tv/podcasts/react-round-up/
👉 Don’t forget to subscribe to this channel for more updates: https://bit.ly/2E7drfJ
👉 Discord server signup: https://discord.gg/ddMZFtTDa5
👉 VS Code theme and font? Night Wolf [black] and JETBrains Mono

source

32 thoughts on “I Was Wrong About Nested React Components”

  1. It's good practice to compose components using the "children" prop from react, specially when nesting components , this prevent unintentional rerenders and it's an easy performance win. Some react engineer tweeted about it recently.

    Reply
  2. I don't understand the earlier part where you said "Folks saying want to hide the implementation of NameField." What does this exactly mean? How does nested NameField component inside App "hide" its implementation?

    Reply
  3. Reusing props and closure maybe the reasons why folks doing that. Basically split complex jsx into smaller pieces and don't have to worry about props drilling for new components

    Reply
  4. @Jack Herrington Why would this be called "nesting"? To me its something more akin to "inline definitions" which is generally a bad idea in JS anywhere unless the function should actually change on each call to its parent (e.g. a callback refering to some unique instance of a class or something)

    Reply
  5. That only applies if the component has its own state tho. I tend to collect state in the highest possible component that does not lead to having to pass props several layers. In this example the state for namefield i would just place into app as well then you would have all state at the same place anyway and can see it all at one glance. Or you use closures and useCallback sure…

    Reply
  6. Hi mike,
    I am having a problem in with masonry layout,

    The issue: i have list of items and a standard masonry view will show those items top to bottom order in columns such as [[1,2,3,4],[5,6,7,8],…rest],

    What i want : i want to show the masonry as left to right such that lastest items stay at top,

    I have done some code for showing it top to left, and it works

    But when the list gets updated the whole layout re renders ,

    So to avoid re rendering of each element, what can be done,
    I am using memo for child elements

    Reply
  7. Great video! I believe there is a way to hack it, by wrapping the nested component in useCallback so whenever the App component rerenders the reference does not change. But as I said it is still a HACK. Love watching your videos about quirks of React. 😃

    Reply
  8. I am again watching this on my phone and I should probably get back at my computer… but I am just a bit lazy and I wanted to watch this on my phone before 😅 an afternoon nap in my bed (I know it’s unusual but my point is that I want to watch this on my phone)… it’s a bit small 😅😅😅 but it is worth watching because it is very interesting…

    Reply
  9. After using vanilla Web Components for over a year and loving it, it still fascinates me how some Framework users are so entrenched, they are perfectly fine fixing issues that only happen because they are using said framework.
    As for nested Web Components, it's basically the best part, and works how you expect it would. No weird surprises, like the one described. Sure it's a bit more verbose, but you can always tell what is going on.

    Reply
  10. The fact that this behaviour is surprising to people shows how superficially even most experienced react developers understand it. I guess that shows just how powerful of an abstraction it really is 🤔

    Reply
  11. Well, you can always default to using useMemo hook, so it doesn’t re-render NameField component on App re-render.

    But please let’s just all agree not to nest components inside of components. That just sounds very confusing to understand, and will make a nightmare to refactor the code later on 😅

    Reply

Leave a Comment