score:110
I figured it out!
After reading this blog post I realized that the placement of this line:
<script src="{% static "build/react.js" %}"></script>
was wrong. That line needs to be the last line in the <body>
section, right before the </body>
tag. Moving the line down solves the problem.
My explanation for this is that react was looking for the id in between the <head>
tags, instead of in the <body>
tags. Because of this it couldn't find the content
id, and thus it wasn't a real DOM element.
score:0
Target container is not a DOM element.
I achieved this error with a simple starter app also.
// index.js
ReactDOM.render(
<Router>
<App />,
document.getElementById('root')
</Router>
);
Solution:
Syntax errors can cause this error. I checked my syntax and wrapped my <App />
properly.
ReactDOM.render(
<Router>
<App />
</Router>,
document.getElementById('root')
);
score:2
I got the same error i created the app with create-react-app but in /public/index.html also added matrialize script but there was to connection with "root" so i added
<div id="root"></div>
just before
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/ materialize.min.js"></script>
And it worked for me .
score:2
One of the case I encountered the same error in a simple project. I hope the solution helps someone.
Below code snippets are sufficient to understand the solution :
index.html
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
someFile.js : Notice the line const portalElement = document.getElementById("overlays"); below :
const portalElement = document.getElementById("overlays");
const Modal = (props) => {
return (
<Fragment>
{ReactDOM.createPortal(<Backdrop />, portalElement)}
{ReactDOM.createPortal(
<ModalOverlay>{props.children}</ModalOverlay>,
portalElement
)}
</Fragment>
);
};
I didn't have any element with id = "overlays" in my index.html file, so the highlighted line above was outputting null and so React wasn't able to find inside which element it should create the portal i.e {ReactDOM.createPortal(<Backdrop />, portalElement)
} so I got below error
I added the div in index.html file and the error was gone.
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="overlays"></div>
<div id="root"></div>
</body>
score:4
Also you can do something like that:
document.addEventListener("DOMContentLoaded", function(event) {
React.renderComponent(
CardBox({url: "/cards/?format=json", pollInterval: 2000}),
document.getElementById("content")
);
})
The DOMContentLoaded event fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.
score:6
For those that implemented react js in some part of the website and encounter this issue. Just add a condition to check if the element exist on that page before you render the react component.
<div id="element"></div>
...
const someElement = document.getElementById("element")
if(someElement) {
ReactDOM.render(<Yourcomponent />, someElement)
}
score:7
I had encountered the same error with React version 16. This error comes when the Javascript that tries to render the React component is included before the static parent dom element in the html. Fix is same as the accepted answer, i.e. the JavaScript should get included only after the static parent dom element has been defined in the html.
score:8
Also, the best practice of moving your <script></script>
to the bottom of the html file fixes this too.
score:11
Just to give an alternative solution, because it isn't mentioned.
It's perfectly fine to use the HTML attribute defer
here. So when loading the DOM, a regular <script>
will load when the DOM hits the script. But if we use defer
, then the DOM and the script will load in parallel. The cool thing is the script gets evaluated in the end - when the DOM has loaded (source).
<script src="{% static "build/react.js" %}" defer></script>
score:20
webpack solution
If you got this error while working in React with webpack and HMR.
You need to create template index.html
and save it in src
folder:
<html>
<body>
<div id="root"></div>
</body>
</html>
Now when we have template with id="root"
we need to tell webpack to generate index.html which will mirror our index.html
file.
To do that:
plugins: [
new HtmlWebpackPlugin({
title: "Application name",
template: './src/index.html'
})
],
template
property will tell webpack how to build index.html
file.
score:53
Also make sure id set in index.html is same as the one you referring to in index.js
index.html:
<body>
<div id="root"></div>
<script src="/bundle.js"></script>
</body>
index.js:
ReactDOM.render(<App/>,document.getElementById('root'));
Source: stackoverflow.com
Related Query
- React and using ReactDOM.createPortal() - Target container is not a DOM element Error
- React Error: Target Container is not a DOM Element
- React Uncaught Error: Target container is not a DOM element
- React - Uncaught Error: Target container is not a DOM element
- React Typescript tests - Target container is not a DOM element
- React 13.3 unmountComponentAtNode() Error: Target container is not a DOM element
- React + Backbone, Target container is not a DOM element
- createRoot(...): Target container is not a DOM element in React Test file
- Target container is not a DOM element using webpack 4 and React
- React Uncaught Error: Invariant Violation: _registerComponent(...): Target container is not a DOM element
- Error: Target container is not a DOM element with React
- Target container is not a DOM element in react
- Enzyme/Jest Testing Error - Invariant Violation: Target container is not a DOM element
- React testing error. Target container is not a DOM element
- Multiple ReactDOM.render() throwing error Target container is not a DOM element
- How to correct React Portal error: Target container is not a DOM element
- React test fails becasue target container is not a DOM element
- Newbie react question: Target container is not a DOM element when calling react from html
- Target container is not a DOM element - React and GatsbyJS - React-slick Component
- React - Target container is not a DOM element
- React simple code error: Target container is not a DOM element
- Target container is not a DOM element - error in importing a function
- Invariant Violation: _registerComponent(...): Target container is not a DOM element
- Testing React: Target Container is not a DOM element
- Target container is not a DOM element React.js
- Test suite failed to run. Invariant Violation: _registerComponent(...): Target container is not a DOM element
- ReactJS - PopUp Component Error: Target container is not a DOM element
- Invariant Violation: Target container is not a DOM element, when element is in the DOM
- ReactJs 0.12 Target container is not a DOM element
- Gastby - ReactDOM: Target container is not a DOM element
More Query from same tag
- Uncontrolled input React
- Background image in div is not taking entire view
- Redux vs custom hook
- SPFx breakpoints and debugging from Visual Studio Code instead from Chrome
- distinction between "() => (" and "() => {"
- Nested React <input> element loses focus on typing
- Electron doesn't launch app after run it in development
- How to initialize a FieldArray from state using redux-form?
- Redux-observable: failed jest test for epic
- React/Javascript - Wait for variable to populate before running function
- React slick compatibility with Nextjs
- How to return multiple values in return while using Redux Dispatch?
- Redux Toolkit, trying to use filter in a reducer function, but it doesn't behave as expected
- react: helping to understand specific currying use case
- how to convert a functions Hook in to class state
- I'm Requesting POST Method via axios but post method is not working
- react shoping cart mutation issue
- Formik and Material-UI
- React editing input element that has a value
- jsx console log inside map inside return statement
- CSS/HTML - Scroll Header before content
- React Spring useSpring hook switching between from state and to state without animation
- Why browser refresh does not display latest changes?
- React - Refreshing Save Counter
- React apollo default query variable from redux store
- React update child component state from parent using getDerivedStateFromProps
- Next.js import with variables or conditionally import
- React state not changing
- Writing on firestore document using cloud functions(using react.js)
- How to test async useEffect?