Jeff Terrell PhD
Aug 28, 2017 · 2 min read

While I appreciate the desire for clarity and craftsmanship in code, and I agree that passing callback functions down the tree isn’t ideal, I’m not sure that this approach is one I would want to use, for a few reasons:

  1. Simple delegators tend to misname the callbacks that the child component needs. For example, a Task component should not have a onTaskComplete callback but rather an onComplete callback. This is a subtle distinction, but an important one for well-factored and conceptually pure components, and using a delegator pattern will tend to bias you away from noticing such things.
  2. As user Chris Geirman noted in his comment, it’s not clear at the call-site what the child-component needs. Passing delegate={this} is bundling some unknown set of methods from this and passing them to the child. I prefer a more explicit interface and generally find that explicit interfaces make code more legible and therefore maintainable.
  3. A better state management approach avoids many of these problems. Flux separates the state from the components by use of “stores,” which store state and emit events when things change. A component that needs some state subscribes to those events and updates itself as needed. So you’re not passing callbacks down the tree. Redux (or more specifically react-redux) provides mapStateToProps and mapDispatchToProps to interface between so-called container and presentational components. Maintaining the conceptual separation between these types of components solves many (though admittedly not necessarily all) of these “over-parenting” problems.

My criticisms aside, thanks for taking the time to write this up. I appreciate the quest for craftsmanship. Cheers!

)
    Jeff Terrell PhD

    Written by

    Professor of the practice of computer science at UNC. Former craftsman at @RoleModelSoft. I don't believe in AI. #clojure #jesusislord

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade