I'm using Next.js with my React app, and I'm having a trouble with creating modal in portal, it throws an error 'Target container is not a DOM element.', here is my code.
import React, { useEffect } from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import classes from './listModal.scss';
const EditorListModal = (props) => {
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 = (
<div className={`${classes['editor-listModal']} ${isShown}`}>
// Modal Content
</div>
);
return ReactDOM.createPortal(element, container);
};
EditorListModal.propTypes = {
show: PropTypes.bool
};
export default React.memo(EditorListModal);
document.body
instead of#__next
? – Clarendon