title: Should you hide modals or remove them from the DOM in React?
body: <p>I'm writing a React application with a lot of modals (but only one ever active at a time with no nested modals), and I'm unsure of which of these two solutions is preferable when it comes to handling showing and hiding these modals:</p>
  
  <ol>
  <li><p>Have a state boolean variable in the parent component that determines whether or not the modal should be shown. If this boolean variable is false, don't render the component. If it's true, render it. Allow the modal to influence this event via a passed in props callback it can leverage. Example snippets:</p>
  
  <pre><code>{ this.state.prompt === "makePW" ? 
    &lt;MakePassword closeModal={this.closePWModal} /&gt; : 
    null
  }
  </code></pre>
  
  <p>Now, within the component, it is always visible and does not control its lifecycle. If it is being rendered then it is visible.</p></li>
  <li><p>Just always show the component in the parent component, like so:</p>
  
  <pre><code>&lt;MakePassword /&gt;
  </code></pre>
  
  <p>Then within the component itself handle its entire lifecycle. That is, the component will have a boolean state variable for its visibility. Some snippets for this approach:</p>
  
  <pre><code>&lt;Modal open={this.state.open} onClose={this.closeModal}&gt;
      &lt;Modal.Header&gt;Header&lt;/Modal.Header&gt;
      &lt;Modal.Content&gt;Body&lt;/Modal.Content&gt;
  &lt;/Modal&gt;
  </code></pre></li>
  </ol>
  
  <p>There are also hybrid approaches, and other solutions as well I'm sure. </p>
  
  <p>Regardless, I suppose the essence of my question is wondering about the preferable solution for showing and hiding modals: is it to always render them and just toggle their visibility, or to actually toggle between adding and removing them from the DOM? And, should the modal itself generally control its lifecycle or should the parent?</p>