Advertisement
Intermediate React, Render Props Pattern

What is a downside of the render props pattern that developers should be aware of?

Short answer

Nesting several render props components inside each other can create deeply indented, harder to read code, sometimes called callback nesting, similar to the wrapper hell problem seen with higher order components.

Save React mock interview
Advertisement
AModel answer
Nesting several render props components inside each other can create deeply indented, harder to read code, sometimes called callback nesting, similar to the wrapper hell problem seen with higher order components. This is one of the main reasons custom hooks became the preferred way to share logic once they were introduced.
// Deeply nested and harder to read
<MouseTracker render={(mouse) => (
  <WindowSize render={(size) => (
    <ScrollPosition render={(scroll) => (
      <Display mouse={mouse} size={size} scroll={scroll} />
    )} />
  )} />
)} />
Real-world example
A team dealing with three levels of nested render props components decides to refactor all three into separate custom hooks instead, flattening the code and making it much easier to read and maintain.
Likely follow-ups
  • How many levels of nesting is generally considered too many before refactoring?
  • How would combining these three examples into custom hooks actually look in code?
Related topics