Can we have a portal in NEXT JS
It's going to break during the ssr because the container
is not initialized. You could try to skip rendering when there is no portal:
return container ? ReactDOM.createPortal(element, container) : null;
Accepted answer is not the best option. You may have lots of render issues when server rendered content differs from the client's. The main idea is to have the same content during SSR and hydration. In your case it would be more accurate to load modal dynamically with { ssr: false }
option.
As the second option take a note at next's example. There they always return null
during initial render both on server and client and that's a correct approach.