'ReactJS - TypeError: Cannot read properties of undefined (reading 'createPortal') - Why?

I am getting the following error when trying to display the modal:

This is the error i am getting

My code is the following: enter image description here

What i am doing wrong?



Solution 1:[1]

The mistake is in import statment.

Replace

import {ReactDOM} from 'react-dom'

with

import ReactDOM from "react-dom" 

Reason : As there is no named export of ReactDOM from "react-dom", it is not imported and the reference will be undefined. So, you are getting the error. ReactDOM is default export.

Solution 2:[2]

For react v18 and above the import statement does have client included

so replacing

import {ReactDOM} from 'react-dom'

with

import ReactDOM from 'react-dom/client';

is advised as per docs on react portals

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dinesh Pandikona
Solution 2 Ryan Pereira