I’m encountering an issue with React’s createRoot function in my project. When I attempt to use ReactDOM.createRoot(document.getElementById('root')).render(<App />);
, I receive the following error in the console: Uncaught Error: createRoot(…): Target container is not a DOM element.
<code>import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App.jsx';
import './index.css';
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
</code>
<code>import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App.jsx';
import './index.css';
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
</code>
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App.jsx';
import './index.css';
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
I have verified that the element with id=”root” exists in my HTML file.
New contributor
Drxlr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.