title: Can we have a portal in Next.js
body: <p>I'm using Next.js with my React app, and I'm having a trouble with creating modal in portal, it throws an error <em>'Target container is not a DOM element.'</em>, here is my code.</p>
  <pre><code>import React, { useEffect } from 'react';
  import ReactDOM from 'react-dom';
  import PropTypes from 'prop-types';
  import classes from './listModal.scss';
  
  const EditorListModal = (props) =&gt; {
    let container;
    if (typeof window !== 'undefined') {
      const rootContainer = document.createElement('div');
      const parentElem = document.querySelector('#__next');
      parentElem.appendChild(rootContainer);
      container = rootContainer;
    }
    const isShown = props.show ? classes['editor-listModal--open'] : '';
    const element = (
      &lt;div className={`${classes['editor-listModal']} ${isShown}`}&gt;
        // Modal Content
      &lt;/div&gt;
    );
  
    return ReactDOM.createPortal(element, container);
  };
  
  EditorListModal.propTypes = {
    show: PropTypes.bool
  };
  
  export default React.memo(EditorListModal);
  </code></pre>