Last updated: Apr 6, 2024
Reading timeยท3 min
The error "createRoot(...): Target container is not a DOM element" occurs for multiple reasons:
id
to the document.getElementById()
method.div
element.index.html
file.Here is an example of how the error occurs.
import {StrictMode} from 'react'; import {createRoot} from 'react-dom/client'; import App from './App'; // ๐๏ธ Passed wrong ID to getElementById() method const rootElement = document.getElementById('wrong-id'); const root = createRoot(rootElement); root.render( <StrictMode> <App /> </StrictMode>, );
We passed the wrong ID to the getElementById
method, so we ended up passing a
null
value to the createRoot()
method which caused the error.
To solve the error, make sure that the ID you are using to select the element is
the same ID that you have specified in your index.html
file and place the
react script below the code that declares the div
element.
<!DOCTYPE html> <html lang="en"> <body> <!-- ๐๏ธ Must match with index.js --> <div id="root"></div> </body> </html>
The ID in the index.html
file in the example is root
, so that's what we have
to use when selecting the root element in index.js
.
import {StrictMode} from 'react'; import {createRoot} from 'react-dom/client'; import App from './App'; // โ Correct ID passed const rootElement = document.getElementById('root'); const root = createRoot(rootElement); root.render( <StrictMode> <App /> </StrictMode>, );
Another common cause of the error is placing the React.js script file above the
code that creates the div
element in the index.html
file.
<!DOCTYPE html> <html lang="en"> <body> <!-- โ๏ธ BAD: script runs before div is created --> <script src="/bundle.js"></script> <div id="root"></div> </body> </html>
The bundle.js
script runs before the div
element is created which causes the
error.
Instead, place your script tag below the code that declares the div
element.
<!DOCTYPE html> <html lang="en"> <body> <div id="root"></div> <!-- โ GOOD: div already exists --> <script src="/bundle.js"></script> </body> </html>
Another common cause of the error is using Webpack with the
html-webpack-plugin
configured incorrectly.
You can use the template
option to provide a custom HTML file when using
Webpack. The
html-webpack-plugin will
automatically inject all necessary CSS, JS, manifest and favicon files into the
markup.
First, update the plugins
section of your Webpack config file providing a path
to your custom html
file.
plugins: [ new HtmlWebpackPlugin({ title: "Your custom title", template: './src/index.html' }) ],
And now create an index.html
file in your src
directory with the following
code.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title><%= htmlWebpackPlugin.options.title %></title> </head> <body> <!-- ๐๏ธ must match the id in your index.js file --> <div id="root"></div> </body> </html>
Now your index.js
file should select the element with an ID of root
.
import {StrictMode} from 'react'; import {createRoot} from 'react-dom/client'; import App from './App'; const rootElement = document.getElementById('root'); const root = createRoot(rootElement); root.render( <StrictMode> <App /> </StrictMode>, );
createRoot()
method takes the root element as a parameter and creates a React root.The root has a render()
method that can be used to render a React element into
the DOM. The root in React is a pointer to the top-level data structure that
React uses to track a tree to render.
Make sure to import createRoot
from react-dom/client
.
Importing it from react-dom is not supported.