title: How to pass conditional children in React / Preact (aka. <If> component)
body: <p>Sometimes I need to create a wrapper element that will show its children (or not) according to its own logic, optionally wrapping them in its own choice of elements:</p>
  
  <pre><code>&lt;SomeWrapper some={condition}&gt;
    Hello
  &lt;/SomeWrapper&gt;
  </code></pre>
  
  <p>This works because the children ("Hello") are static. But what if the children are to be computed dynamically and <strong><em>may only be well-defined when the condition holds?</em></strong></p>
  
  <pre><code>&lt;SomeWrapper some={condition}&gt;
    &lt;ul&gt;
      {this.may.not.exist.unless.condition.map(item =&gt; 
        &lt;li key={item.id}&gt;{item.text}&lt;/li&gt;
      )}
    &lt;/ul&gt;
  &lt;/SomeWrapper&gt;
  </code></pre>
  
  <p>Here, if the condition is false and the wrapper element does not make use of its children, they will still be created and passed down the tree, wasting resources and possibly throwing an error in the process.</p>
  
  <p>One solution (probably the best?) is to wrap the contents in their own component:</p>
  
  <pre><code>&lt;SomeWrapper some={condition}&gt;
    &lt;InnerContent/&gt;
  &lt;/SomeWrapper&gt;
  </code></pre>
  
  <p>This works because (AFAIK, correct me if I'm wrong) InnerContent's constructor and render will not be called unless SomeWrapper actually decides to make use of its <code>children</code> prop.</p>
  
  <p>But what if I don't want to create a component for 3 lines of code?</p>
  
  <p>I have seen two options in the wild, none of which are particularly appealing:</p>
  
  <ol>
  <li><p>passing a thunk as the only child:</p>
  
  <pre><code>&lt;SomeWrapper some={condition}&gt;{() =&gt;
    &lt;ul&gt;  
      {this.may.not.exist.unless.condition.map(item =&gt; 
        &lt;li key={item.id}&gt;{item.text}&lt;/li&gt;
      )}
    &lt;/ul&gt;
  }&lt;/SomeWrapper&gt;
  </code></pre></li>
  <li><p>passing a thunk as a prop:</p>
  
  <pre><code>&lt;SomeWrapper some={condition} render={() =&gt;
    &lt;ul&gt;  
      {this.may.not.exist.unless.condition.map(item =&gt; 
        &lt;li key={item.id}&gt;{item.text}&lt;/li&gt;
      )}
    &lt;/ul&gt;
  }/&gt;
  </code></pre></li>
  </ol>
  
  <p>I don't like them because the lambda adds visual noise to the code, not to mention wasting resources, being re-created at every <code>render()</code> execution (AFAIK.)</p>
  
  <p>Is there any other solution I'm not seeing? Should I always go with the InnerContent element?</p>