title: React: override internal components with custom component
body: <p>I have a modal that is completely self contained. The modal is opened via going to the modal route and all the functionality to close the modal from button or outside clicks is within the modal component. Basically the modal is not controlled by any parent passing state. I was given a task of making the modals button customizable, meaning passing in a new button component, so we can add the modal to our lib instead of copy pasting the code in projects. Lol this seemed simple enough, and maybe it is and I am just overthinking this.</p>
  <p>I cant paste the actual code but I can use a contrived example. This is a very simplified version of the modal, keeping in mind it opens via route so there's really no state and setState in the actual code. Also here is a <a href="https://jsfiddle.net/aaronbalthaser/af5p2kvj/73/" rel="nofollow noreferrer">fiddle</a></p>
  <pre><code>const ModalHeader = ({ onClose }) =&gt; {
    return (
      &lt;div className=&quot;modal__header&quot;&gt;
        &lt;button
          className=&quot;modal__close-btn&quot;
          data-testid=&quot;modal-close-button&quot;
          onClick={onClose}
        /&gt;
      &lt;/div&gt;
    );
  };
  
  const Modal = ({ children }) =&gt; {
    const [state, setState] = React.useState(true);
  
    const handleCloseOutsideClick = () =&gt; {
      setState(false);
    };
  
    const handleCloseButtonClick = () =&gt; {
      setState(false);
    };
  
    const renderModal = () =&gt; {
      return (
        &lt;div className=&quot;modal-overlay&quot; onClick={handleCloseOutsideClick}&gt;
          &lt;div className=&quot;modal&quot;&gt;
            &lt;ModalHeader onClose={handleCloseButtonClick} /&gt;
            {children}
          &lt;/div&gt;
        &lt;/div&gt;
      );
    };
  
    return state ? renderModal() : null;
  };
  
  const App = () =&gt; {
    return (
      &lt;Modal&gt;
        &lt;div&gt;Modal Children&lt;/div&gt;
      &lt;/Modal&gt;
    );
  };
  
  ReactDOM.render(&lt;App /&gt;, document.querySelector('#app'));
  </code></pre>
  <p>I tried a few things, initially I attempted to find a way to pass in a new header component containing a button. Then as I got into the code I realized what I was doing would lose the self contained functionality of the modal. My approach was along the lines of below but obviously the onClick would be an issue since invoking the close functionality is internal.</p>
  
    
      
        
      
    
  
  <p>So I tried using cloneElement to add props within the component if the custom header was detected:</p>
  <pre><code>// inside modal component
  React.useEffect(() =&gt; {
    React.Children.map(children, (child: React.ReactElement) =&gt; {
      if (child &amp;&amp; child.type === ModalHeader) {
        setHederFound(true);
      }
    });
  }, []);
  
  // inside modal render:
  &lt;div className={modalClasses} onClick={stopPropagation}&gt;
    {!headerFound ? (
      &lt;ModalDefaultHeader onClose={handleCloseButtonClick} /&gt;
    ) : (
      React.Children.map(children, (child: React.ReactElement) =&gt; {
        if (child &amp;&amp; child.type === ModalHeader) {
          return React.cloneElement(child, {
            onClose: handleCloseButtonClick,
          });
        }
      })
    )}
    {children}
  &lt;/div&gt;;
  </code></pre>
  <p>Obviously that did not work because there's no onClick in the custom button. Anyways I am thinking that I am over complicating this. I just need a way to pass in a custom button while leaving the functionality internal to the modal. Any assistance would be appreciated.</p>
  <p>Thanks in advance.</p>