<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Merrick Christensen on Medium]]></title>
        <description><![CDATA[Stories by Merrick Christensen on Medium]]></description>
        <link>https://medium.com/@iammerrick?source=rss-206e78c9e784------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*yCXSXZFilYe0iwc3.jpeg</url>
            <title>Stories by Merrick Christensen on Medium</title>
            <link>https://medium.com/@iammerrick?source=rss-206e78c9e784------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 13:56:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@iammerrick/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Headless User Interface Components]]></title>
            <link>https://medium.com/merrickchristensen/headless-user-interface-components-565b0c0f2e18?source=rss-206e78c9e784------2</link>
            <guid isPermaLink="false">https://medium.com/p/565b0c0f2e18</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[user-interface]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[design-systems]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Merrick Christensen]]></dc:creator>
            <pubDate>Sat, 23 Jun 2018 17:05:00 GMT</pubDate>
            <atom:updated>2018-06-23T17:05:00.697Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://www.merrickchristensen.com/articles/headless-user-interface-components/">Original Article</a></p><p>A headless user interface component is a component that offers maximum visual flexibility by providing no interface. “Wait for a second, are you advocating a user interface pattern that doesn’t have a user interface?”</p><p>Yes. That is exactly what I’m advocating.</p><h3>Coin Flip Component</h3><p>Suppose you had a requirement to implement a coin flip feature that performed some logic when it is rendered to emulate a coin flip! 50% of the time the component should render “Heads” and 50% of the time it should render “Tails”. You say to your product manager, “Oof that will take years of research!”, and you get to work.</p><pre>const CoinFlip = () =&gt;<br> Math.random() &lt; 0.5 ? &lt;div&gt;Heads&lt;/div&gt; : &lt;div&gt;Tails&lt;/div&gt;;</pre><p>Turns out emulating coin flips is way easier than you thought so you proudly share the results. You get a response, “This is great! Could you please update it to show these cool coin images?”. No problem!</p><pre><br>const CoinFlip = () =&gt;<br> Math.random() &lt; 0.5 ? (<br>   &lt;div&gt;<br>     &lt;img src=”/heads.svg” alt=”Heads” /&gt;<br>   &lt;/div&gt;<br> ) : (<br>   &lt;div&gt;<br>     &lt;img src=”/tails.svg” alt=”Tails” /&gt;<br>   &lt;/div&gt;<br> );</pre><p>Soon, they’d like to use your &lt;CoinFlip /&gt; component in the marketing material to show people how cool your new feature is. “We’d like to put in the blog post, but we need the labels “Heads” &amp; “Tails” back, for SEO and stuff.” Oh man, I guess we’ll add a flag for the marketing site?</p><pre>const CoinFlip = (<br> // We’ll default to false to avoid breaking the applications<br> // current usage.<br> { showLabels = false }<br>) =&gt;<br> Math.random() &lt; 0.5 ? (<br>   &lt;div&gt;<br>     &lt;img src=”/heads.svg” alt=”Heads” /&gt;</pre><pre>     {/* Add these labels for the marketing site. */}<br>     {showLabels &amp;&amp; &lt;span&gt;Heads&lt;/span&gt;}<br>   &lt;/div&gt;<br> ) : (<br>   &lt;div&gt;<br>     &lt;img src=”/tails.svg” alt=”Tails” /&gt;</pre><pre>     {/* Add these labels for the marketing site. */}<br>     {showLabels &amp;&amp; &lt;span&gt;Tails&lt;/span&gt;}<br>   &lt;/div&gt;<br> );</pre><p>Later, a requirement emerges. “We were wondering if you could add a button to &lt;CoinFlip /&gt;, but only in the application, to rerun the odds?”. Things are starting to get ugly, I can’t even look Kent C. Dodds in the eyes anymore:</p><pre><br>const flip = () =&gt; ({<br>  flipResults: Math.random()<br>});</pre><pre>class CoinFlip extends React.Component {<br>  static defaultProps = {<br>    showLabels: false,<br>    // We don’t repurpose `showLabels`, we aren’t animals, after all.<br>    showButton: false<br>  };</pre><pre>  state = flip();</pre><pre>  handleClick = () =&gt; {<br>    this.setState(flip);<br>  };</pre><pre>  render() {<br>   return (<br>     // Use fragments so people take me seriously.<br>     &lt;&gt;<br>     {this.state.showButton &amp;&amp; (<br>       &lt;button onClick={this.handleClick}&gt;Reflip&lt;/button&gt;<br>     )}<br>     {this.state.flipResults &lt; 0.5 ? (<br>       &lt;div&gt;<br>         &lt;img src=”/heads.svg” alt=”Heads” /&gt;<br>         {showLabels &amp;&amp; &lt;span&gt;Heads&lt;/span&gt;}<br>       &lt;/div&gt;<br>     ) : (<br>       &lt;div&gt;<br>         &lt;img src=”/tails.svg” alt=”Tails” /&gt;<br>         {showLabels &amp;&amp; &lt;span&gt;Tails&lt;/span&gt;}<br>       &lt;/div&gt;<br>     )}<br>     &lt;/&gt;<br>   );<br> }<br>}</pre><p>Soon a co-worker reaches out to you. “Hey, your &lt;CoinFlip /&gt; feature is rad! We just got assigned the new &lt;DiceRoll /&gt; feature and we’d like to reuse your code!”. The new dice feature:</p><p>1. Wants to “re-run” the odds onClick.<br>2. Wants to be displayed in the application and marketing site as well.<br>3. Has a totally different interface.<br>4. Has different odds.</p><p>You now have two options, replying “Sorry, not much to share here.” or adding DiceRoll complexity into CoinFlip as you watch the bones of your component break under the weight of its responsibility. (Is there a market for brooding programmer poets? I’d love to pursue that craft.)</p><h3>Enter Headless Components</h3><p>Headless user interface components separate the logic &amp; behavior of a component from its visual representation. This pattern works great when the logic of a component is sufficiently complex and decoupled from its visual representation. A headless implementation of &lt;CoinFlip/&gt; as <a href="https://www.merrickchristensen.com/articles/function-as-child-components/">function as child component</a> or render prop would look like so:</p><pre><br>const flip = () =&gt; ({<br>  flipResults: Math.random()<br>});</pre><pre>class CoinFlip extends React.Component {<br>  state = flip();</pre><pre>  handleClick = () =&gt; {<br>    this.setState(flip);<br>  };</pre><pre>  render() {<br>    return this.props.children({<br>      rerun: this.handleClick,<br>      isHeads: this.state.flipResults &lt; 0.5<br>    });<br>  }<br>}</pre><p>This component is headless because it doesn’t render anything, it expects the various consumers to do the presentation work while it tackles the logic lifting. So the application code would look like so:</p><pre>&lt;CoinFlip&gt;<br>  {({ rerun, isHeads }) =&gt; (<br>   &lt;&gt;<br>     &lt;button onClick={rerun}&gt;Reflip&lt;/button&gt;<br>     {isHeads ? (<br>       &lt;div&gt;<br>         &lt;img src=”/heads.svg” alt=”Heads” /&gt;<br>       &lt;/div&gt;<br>     ) : (<br>       &lt;div&gt;<br>         &lt;img src=”/tails.svg” alt=”Tails” /&gt;<br>       &lt;/div&gt;<br>     )}<br>   &lt;/&gt;<br>  )}<br>&lt;/CoinFlip&gt;</pre><p>The marketing website code:</p><pre>&lt;CoinFlip&gt;<br> {({ isHeads }) =&gt; (<br>   &lt;&gt;<br>     {isHeads ? (<br>       &lt;div&gt;<br>         &lt;img src=”/heads.svg” alt=”Heads” /&gt;<br>         &lt;span&gt;Heads&lt;/span&gt;<br>       &lt;/div&gt;<br>     ) : (<br>       &lt;div&gt;<br>         &lt;img src=”/tails.svg” alt=”Tails” /&gt;<br>         &lt;span&gt;Tails&lt;/span&gt;<br>       &lt;/div&gt;<br>     )}<br>   &lt;/&gt;<br> )}<br>&lt;/CoinFlip&gt;</pre><p>Isn’t this great! We’ve completely untangled the logic from the presentation! This gives us so much visual flexibility! I know what you’re thinking…</p><blockquote>You mindless sack of idiot! Isn’t that just a render prop?!</blockquote><p>This headless component happens to be implemented as a render prop, yes! It could just as well be implemented as a higher order component. <em>Looks over my shoulder, in a hushed low tone.</em> It could have even been implemented as a View and a Controller. Or a ViewModel and a View. The point here is about separating the “mechanism” of flipping coins and the “interface” to that mechanism.</p><h4>What about &lt;DiceRoll /&gt;?</h4><p>The neat thing about this separation is how easy it is to generalize our headless component to support our co-workers new &lt;DiceRoll /&gt; feature. Hold my Diet Coke™:</p><pre>const run = () =&gt; ({<br>  random: Math.random()<br>});</pre><pre>class Probability extends React.Component {<br>  state = run();</pre><pre>  handleClick = () =&gt; {<br>    this.setState(run);<br>  };</pre><pre>  render() {<br>    return this.props.children({<br>      rerun: this.handleClick,</pre><pre>      // By taking in a threshold property we can support<br>      // different odds!<br>      result: this.state.random &lt; this.props.threshold<br>    });<br>  }<br>}<br></pre><p>With this headless component we can swap out the implementation of &lt;CoinFlip /&gt; without any changes to its consumers:</p><pre>const CoinFlip = ({ children }) =&gt; (<br> &lt;Probability threshold={0.5}&gt;<br>   {({ rerun, result }) =&gt;<br>     children({<br>       isHeads: result,<br>       rerun<br>   })}<br> &lt;/Probability&gt;<br>);</pre><p>Now our co-worker can share the mechanism of our &lt;Probability /&gt; emulator!</p><pre>const RollDice = ({ children }) =&gt; (<br>  // Six Sided Dice<br>  &lt;Probability threshold={1 / 6}&gt;<br>    {({ rerun, result }) =&gt; (<br>      &lt;div&gt;<br>        {/* She was able to use a different event! */}<br>        &lt;span onMouseOver={rerun}&gt;Roll the dice!&lt;/span&gt;<br>        {/* Totally different interface! */}<br>        {result ? (<br>          &lt;div&gt;Big winner!&lt;/div&gt;<br>        ) : (<br>          &lt;div&gt;You win some, you lose most.&lt;/div&gt;<br>        )}<br>      &lt;/div&gt;<br>    )}<br> &lt;/Probability&gt;<br>);</pre><p>Pretty neat, eh?</p><h3>Rule of Separation — Unix Philosophy</h3><p>This is one expression of a general underlying principle, one that has been around for a very long time! Rule 4 of the “Basics of Unix Philosophy” is:</p><blockquote>Rule of Separation: Separate policy from mechanism; separate interfaces from engines. — Eric S. Raymond</blockquote><p>I’d like to extract a portion of that book and replace the word “policy” with “interface”.</p><blockquote>_Interface_ and mechanism tend to mutate on different timescales, with _interfaces_ changing much faster than mechanism. Fashions in the look and feel of GUI toolkits may come and go, but raster operations and compositing are forever.</blockquote><blockquote>Thus, hardwiring _interfaces_ and mechanisms together has two bad effects: It makes _interfaces_ rigid and harder to change in response to user requirements, and it means that trying to change _interfaces_ has a strong tendency to destabilize the mechanism.</blockquote><blockquote>On the other hand, by separating the two we make it possible to experiment with new _interfaces_ without breaking mechanism. We also make it much easier to write good tests for the mechanism (_interfaces_, because _they_ age so quickly, often do not justify the investment).</blockquote><p>I love the great insights here! This also gives us some insight as to when it is useful to use the headless component pattern.</p><ol><li>How long will this component live for? Is it worth deliberately preserving the mechanism aside from the interface? Perhaps to use this mechanism in another project with a different look and feel?</li><li>How frequently is our interface bound to change? Will the same mechanism have multiple interfaces?</li></ol><p>There is an indirection cost paid when you separate “mechanism” and “policy”. You need to be sure that the benefits of separation merit the expense of indirection. I think this is largely where a lot of the MV* patterns of the past went wrong, they started with the axiom that <em>everything</em> should be separated this way; when in reality, mechanism and policy are often deeply coupled or the cost of separation doesn’t outweigh the benefits of this sort of separation.</p><h3>Open Source Headless Components &amp; Non-Trivial References</h3><p>For a truly exemplar non-trivial headless component, check out a project by my friend <a href="https://kentcdodds.com/">Kent C. Dodds</a> over at Paypal called <a href="https://github.com/paypal/downshift">downshift</a>. In fact, it is downshift that ultimately inspired this post. Without providing any user interface, downshift offers sophisticated autocomplete/dropdown/select experiences that are accessible. Take a look at all the ways it can be used <a href="http://downshift.netlify.com/?selectedKind=Examples&amp;selectedStory=basic&amp;full=0&amp;addons=1&amp;stories=1&amp;panelRight=0">here</a>.</p><p>I sincerely hope that more projects like downshift emerge over time. I can’t count how many times I’ve wanted to use a particular open source UI component but couldn’t because it wasn’t “themeable” or “skinnable” in the way that met design requirements. Headless components circumvent this problem entirely with a “bring your own interface” requirement.</p><p>In a world where design systems and user interface libraries are headless, your interfaces can have a high-end custom feel _and_ the durability &amp; accessibility of a great open source library. You spend your time implementing the only part that you needed to, the part that is truly unique, the look and feel specific to your application.</p><p>I could go on about the benefits from internationalization to E2E test integration but I’d recommend you try it out for yourself.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=565b0c0f2e18" width="1" height="1" alt=""><hr><p><a href="https://medium.com/merrickchristensen/headless-user-interface-components-565b0c0f2e18">Headless User Interface Components</a> was originally published in <a href="https://medium.com/merrickchristensen">Merrick Christensen</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Though my work may be menial, though my contribution may be small, I can perform it with dignity…]]></title>
            <link>https://medium.com/merrickchristensen/though-my-work-may-be-menial-though-my-contribution-may-be-small-i-can-perform-it-with-dignity-98f94d21ccdd?source=rss-206e78c9e784------2</link>
            <guid isPermaLink="false">https://medium.com/p/98f94d21ccdd</guid>
            <category><![CDATA[work]]></category>
            <category><![CDATA[principles]]></category>
            <category><![CDATA[services]]></category>
            <category><![CDATA[humility]]></category>
            <dc:creator><![CDATA[Merrick Christensen]]></dc:creator>
            <pubDate>Mon, 08 Aug 2016 21:16:57 GMT</pubDate>
            <atom:updated>2016-08-08T21:16:57.117Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><em>Though my work may be menial, though my contribution may be small, I can perform it with dignity and offer it with unselfishness. My talents may not be great, but I can use them to bless the lives of others. I can be one who does his work with pride in that which comes from hand and mind. I can be one who works with respect for my associates, for their opinions, for their beliefs, with appreciation for their problems and with a desire to help them should they stumble. I believe in the principle that I can make a difference in this world. It may be ever so small. But it will count for the greater good. The goodness of the world in which we live is the accumulated goodness of many small and seemingly inconsequential acts. — Gordon B Hinkley</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=98f94d21ccdd" width="1" height="1" alt=""><hr><p><a href="https://medium.com/merrickchristensen/though-my-work-may-be-menial-though-my-contribution-may-be-small-i-can-perform-it-with-dignity-98f94d21ccdd">Though my work may be menial, though my contribution may be small, I can perform it with dignity…</a> was originally published in <a href="https://medium.com/merrickchristensen">Merrick Christensen</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Function as Child Components]]></title>
            <link>https://medium.com/merrickchristensen/function-as-child-components-5f3920a9ace9?source=rss-206e78c9e784------2</link>
            <guid isPermaLink="false">https://medium.com/p/5f3920a9ace9</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Merrick Christensen]]></dc:creator>
            <pubDate>Sat, 30 Jul 2016 20:52:03 GMT</pubDate>
            <atom:updated>2017-09-25T16:34:44.931Z</atom:updated>
            <content:encoded><![CDATA[<p>I recently polled on Twitter regarding Higher Order Components and Function as Child, the results were surprising to me.</p><h3>Merrick Christensen on Twitter</h3><p>Which API do you prefer? Why? https://gist.github.com/iammerrick/2d7b0398eae175d14687745bf95eda72 ...</p><p>Though I quite appreciated the response from my friend Ryan Florence:</p><h3>Ryan Florence on Twitter</h3><p>@iammerrick function as child every time. I can&#39;t think of a scenario that HoC is more capable yet.</p><p>Classic Ryan, “Rethinking Best Practices” anyone? If you don’t know what the “Function as Child” pattern is, this article is my attempt to:</p><ol><li>Teach you what it is.</li><li>Convince you of why it is useful.</li><li>Get some fetching hearts, or retweets or likes or newsletters or something, I don’t know. I just want to feel appreciated, you know?</li></ol><h3>What are Function as Child Components?</h3><p>“Function as Child Component”s are components that receive a function as their child. The pattern is simply implemented and enforced thanks to React’s property types.</p><pre>class MyComponent extends React.Component { <br>  render() {<br>    return (<br>      &lt;div&gt;<br>        {this.props.children(&#39;Scuba Steve&#39;)}<br>      &lt;/div&gt;<br>    );<br>  }<br>}</pre><pre>MyComponent.propTypes = {<br>  children: React.PropTypes.func.isRequired,<br>};</pre><p>That is it! By using a Function as Child Component we decouple our parent component and our child component letting the composer decide what &amp; how to apply parameters to the child component. For example:</p><pre>&lt;MyComponent&gt;<br>  {(name) =&gt; (<br>    &lt;div&gt;{name}&lt;/div&gt;<br>  )}<br>&lt;/MyComponent&gt;</pre><p>And somebody else, using the same component could decide to apply the name differently, perhaps to an attribute:</p><pre>&lt;MyComponent&gt;<br>  {(name) =&gt; (<br>    &lt;img src=’/scuba-steves-picture.jpg’ alt={name} /&gt;<br>  )}<br>&lt;/MyComponent&gt;</pre><p>What is really neat here is that MyComponent, the Function as Child Component can manage state on behalf of components it is composed with, without making demands on how that state is leveraged by its children. Lets move on to a more realistic example.</p><h4>The Ratio Component</h4><p>The Ratio Component will use the current device width, listen for resize events and call into its children with a width, height and some information about whether or not it has computed the size yet.</p><p>First we start out with a Function as Child Component snippet, this is common across all Function as Child Component’s and it just lets consumers know we are expecting a function as our child, not React nodes.</p><pre>class Ratio extends React.Component {<br>  render() {<br>    return (<br>        {this.props.children()}<br>    );<br>  }<br>}</pre><pre>Ratio.propTypes = {<br> children: React.PropTypes.func.isRequired,<br>};</pre><p>Next lets design our API, we want a ratio provided in terms of X and Y axis which we will then use the current width to compute, lets set up some internal state to manage the width and height, whether or not we have even calculated that yet, along with some propTypes and defaultProps to be good citizens for people using our component.</p><pre>class Ratio extends React.Component {<br>  constructor() {<br>    super(...arguments);<br>    this.state = {<br>      hasComputed: false,<br>      width: 0,<br>      height: 0, <br>    };<br>  }</pre><pre>  render() {<br>    return (<br>      {this.props.children()}<br>    );<br>  }<br>}</pre><pre>Ratio.propTypes = {<br>  x: React.PropTypes.number.isRequired,<br>  y: React.PropTypes.number.isRequired,<br>  children: React.PropTypes.func.isRequired,<br>};</pre><pre>Ratio.defaultProps = {<br>  x: 3,<br>  y: 4<br>};</pre><p>Alright so we aren’t doing anything interesting yet, lets add some event listeners and actually calculate the width (accommodating as well for when our ratio changes):</p><pre>class Ratio extends React.Component {<br>  constructor() {<br>    super(...arguments);<br>    this.handleResize = this.handleResize.bind(this);<br>    this.state = {<br>      hasComputed: false,<br>      width: 0,<br>      height: 0, <br>    };<br>  }</pre><pre>  getComputedDimensions({x, y}) {<br>    const {width} = this.container.getBoundingClientRect();</pre><pre>return {<br>      width,<br>      height: width * (y / x), <br>    };<br>  }</pre><pre>  componentWillReceiveProps(next) {<br>    this.setState(this.getComputedDimensions(next));<br>  }</pre><pre>  componentDidMount() {<br>    this.setState({<br>      ...this.getComputedDimensions(this.props),<br>      hasComputed: true,<br>    });</pre><pre>    window.addEventListener(&#39;resize&#39;, this.handleResize, false);<br>  }</pre><pre>  componentWillUnmount() {<br>    window.removeEventListener(&#39;resize&#39;, this.handleResize, false);<br>  }</pre><pre>  handleResize() {<br>    this.setState({<br>      hasComputed: false,<br>    }, () =&gt; {<br>      this.setState({<br>        hasComputed: true,<br>        ...this.getComputedDimensions(this.props),<br>      });<br>    });<br>  }</pre><pre>render() {<br>    return (<br>      &lt;div ref={(ref) =&gt; this.container = ref}&gt;<br>        {this.props.children(this.state.width, this.state.height, this.state.hasComputed)}<br>      &lt;/div&gt;<br>    );<br>  }<br>}</pre><pre>Ratio.propTypes = {<br>  x: React.PropTypes.number.isRequired,<br>  y: React.PropTypes.number.isRequired,<br>  children: React.PropTypes.func.isRequired,<br>};</pre><pre>Ratio.defaultProps = {<br>  x: 3,<br>  y: 4<br>};</pre><p>Alright, so I did a lot there. We added some event listeners to listen for resize events as well as actually computing the width and height using the provided ratio. Neat, so we’ve got a width and height in our internal state, how can we share it with other components?</p><p>This is one of those things that is hard to understand because it is so simple that when you see it you think, “That can’t be all there is to it.” but this <strong><em>is </em></strong>all there is to it.</p><h4>Children is literally just a JavaScript function.</h4><p>That means in order to pass the calculated width and height down we just provide them as parameters:</p><pre>render() {<br>    return (<br>      &lt;div ref=&#39;container&#39;&gt;<br>        {this.props.children(this.state.width, this.state.height, this.state.hasComputed)}<br>      &lt;/div&gt;<br>    );<br>}</pre><p>Now anyone can use the ratio component to provide a full width and properly computed height in whatever way they would like! For example, someone could use the Ratio component for setting the ratio on an img:</p><pre>&lt;Ratio&gt;<br>  {(width, height, hasComputed) =&gt; (<br>    hasComputed <br>      ? &lt;img src=&#39;/scuba-steve-image.png&#39; width={width} height={height} /&gt; <br>      : null<br>  )}<br>&lt;/Ratio&gt;</pre><p>Meanwhile, in another file, someone has decided to use it for setting CSS properties.</p><pre>&lt;Ratio&gt;<br>  {(width, height, hasComputed) =&gt; (<br>    &lt;div style={{width, height}}&gt;Hello world!&lt;/div&gt;<br>  )}<br>&lt;/Ratio&gt;</pre><p>And in another app, someone is using to conditionally render different children based on computed height:</p><pre>&lt;Ratio&gt;<br>  {(width, height, hasComputed) =&gt; (<br>    hasComputed &amp;&amp; height &gt; TOO_TALL<br>      ? &lt;TallThing /&gt;<br>      : &lt;NotSoTallThing /&gt;<br>  )}<br>&lt;/Ratio&gt;</pre><h3>Strengths</h3><ol><li>The developer composing the components owns how these properties are passed around and used.</li><li>The author of the Function as Child Component doesn’t enforce how its values are leveraged allowing for very flexible use.</li><li>Consumers don’t need to create another component to decide how to apply properties passed in from a “Higher Order Component”. Higher Order Components typically enforce property names on the components they are composed with. To work around this many providers of “Higher Order Components” provide a selector function which allows consumers to choose your property names (think redux-connects select function). This isn’t a problem with Function as Child Components.</li><li>Doesn’t pollute “props” namespace, this allows you to use a “Ratio” component and a “Pinch to Zoom” component together regardless that they are both calculating width. Higher Order Components carry an implicit contract they impose on the components they are composed with, unfortunately this can mean colliding prop names being unable to compose Higher Order Components with other ones.</li><li>Higher Order Components create a layer of indirection in your development tools and components themselves, for example setting constants on a Higher Order Component will be unaccessible once wrapped in a Higher Order Component. For example:</li></ol><pre>MyComponent.SomeContant = ‘SCUBA’;</pre><p>Then wrapped by a Higher Order Component,</p><pre>export default connect(...., MyComponent);</pre><p>RIP your constant. It is no longer accessible without the Higher Order Component providing a function to access the underlying component class. Sad.</p><h4>Summary</h4><p>Most the time when you think “I need a Higher Order Component for this shared functionality!” I hope I have convinced you that a Function as Child Component is a better alternative for abstracting your UI concerns, in my experience it nearly always is, with the exception that your child component is truly coupled to the Higher Order Component it is composed with.</p><h4>An Unfortunate Truth About Higher Order Components</h4><p>As an ancillary point, I believe that Higher Order Components are improperly named though it is probably to late to try and change their name. A higher order function is a function that does at least one of the following:</p><ol><li>Takes n functions as arguments.</li><li>Returns a function as a result.</li></ol><p>Indeed Higher Order Components do something similar to this, namely take a Component as and argument and return a Component but I think it is easier to think of a Higher Order Component as a factory function, it is a function that dynamically creates a component to allow for runtime composition of your components. However, they are <strong>unaware</strong> of your React state and props at composition time!</p><p>Function as Child Components allow for similar composition of your components with the benefit of having access to state, props and context when making composition decisions. Since Function as Child Components:</p><ol><li>Take a function as an argument.</li><li>Render the result of said function.</li></ol><p>I can’t help but feel they should have gotten the title “Higher Order Components” since it is a lot like higher order functions only using the component composition technique instead of functional composition. Oh well, for now we will keep calling them “Function as Child Components” which is just wordy and gross sounding.</p><h3>Examples</h3><ol><li><a href="https://gist.github.com/iammerrick/c4bbac856222d65d3a11dad1c42bdcca">Pinch to Zoom — Function as Child Component</a></li><li><a href="https://github.com/chenglou/react-motion">react-motion </a>— This project introduced me to this concept after being a long time Higher Order Component convert.</li></ol><h3>The Ugly</h3><p>Since Function as Child Components give you this power at render time, you typically can’t optimize them using shouldComponentUpdate without hindering your composition ability.</p><h3>Dan Abramov on Twitter</h3><p>@ryanflorence @iammerrick When you want it to work fast, HOCs might be better. F-as-a-child can&#39;t be optimized because it changes on render</p><p>However, even Dan Abramov, the Benevolent Leader Of Whatever We Are Going To Be Doing Next, has acknowledged the grey area:</p><h3>Dan Abramov on Twitter</h3><p>@iammerrick @ryanflorence In many cases it doesn&#39;t matter though. react-motion uses this pattern and works fine.</p><p>I have personally not found it a hinderance to our application’s performance since Function as Child Components are passthrough components to be composed with children it doesn’t know about anyhow. Higher Order Components find themselves in a similar situation as they are often designed to take unknown properties as well and therefore need to do “as good as we can or pass through” optimizations such as shallow equals which you can find in <a href="https://github.com/reactjs/react-redux/blob/master/src/components/connect.js#L216-L218">react-redux</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5f3920a9ace9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/merrickchristensen/function-as-child-components-5f3920a9ace9">Function as Child Components</a> was originally published in <a href="https://medium.com/merrickchristensen">Merrick Christensen</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Plague of the Industrialist]]></title>
            <link>https://medium.com/merrickchristensen/the-plague-of-the-industrialist-f1a8f81088e1?source=rss-206e78c9e784------2</link>
            <guid isPermaLink="false">https://medium.com/p/f1a8f81088e1</guid>
            <category><![CDATA[design]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[development]]></category>
            <dc:creator><![CDATA[Merrick Christensen]]></dc:creator>
            <pubDate>Tue, 17 Nov 2015 21:24:13 GMT</pubDate>
            <atom:updated>2015-11-17T21:24:13.423Z</atom:updated>
            <content:encoded><![CDATA[<p>The industrial revolution taught us about assembly lines, steps, measurement and process. It taught us to maximize output and drive down costs. It taught us to maximize efficiency and cut down excess waste. These can all be good things rightfully applied, but they’ve never been good things for creativity and art.</p><p>The industrialist’s mindset has leaked its way into everything, particularly software development. Designers are viewed as machines who take ideas and translate them into two dimensional space. Developers are the human compilers who turn those designs into code. We have “backlogs” for the assembly line, “requirements” as instructions for the design machine and “acceptance criteria” for the human compiler. So much so that many of these roles are simply sourced to a third party, typically a third party who cares little to nothing about the product, or the problem that product is trying to solve.</p><p>Software is an art form. It isn’t meant to be created using the industrial machine. Consider the major players in software today… Facebook, Instagram, Google, Windows, Linux, OSX… they all had their roots planted by someone who was deeply invested in solving a problem. Not with some assembly line of programmers contented to live a life creating someone else’s dream. No it was their dream. Their problem to solve.</p><p>Software is art. Treat it like one.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f1a8f81088e1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/merrickchristensen/the-plague-of-the-industrialist-f1a8f81088e1">The Plague of the Industrialist</a> was originally published in <a href="https://medium.com/merrickchristensen">Merrick Christensen</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>