126
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.
59
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'));
- [Django]-Using Cloudfront with Django S3Boto
- [Django]-How do you configure Django to send mail through Postfix?
- [Django]-How to write setup.py to include a Git repository as a dependency
23
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.
- [Django]-Reload django object from database
- [Django]-How do I remove Label text in Django generated form?
- [Django]-Querying django migrations table
13
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>
- [Django]-How do you skip a unit test in Django?
- [Django]-How to read the database table name of a Model instance?
- [Django]-What's the purpose of Django setting βSECRET_KEYβ?
10
Also, the best practice of moving your <script></script>
to the bottom of the html file fixes this too.
- [Django]-How to get Django and ReactJS to work together?
- [Django]-Django TypeError: 'RelatedManager' object is not iterable
- [Django]-Filtering using viewsets in django rest framework
10
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)
}
- [Django]-What is the purpose of adding to INSTALLED_APPS in Django?
- [Django]-Celery discover tasks in files with other filenames
- [Django]-Django: multiple models in one template using forms
8
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.
- [Django]-How can I call a custom Django manage.py command directly from a test driver?
- [Django]-Django REST Framework β 405 METHOD NOT ALLOWED using SimpleRouter
- [Django]-Django staticfiles not found on Heroku (with whitenoise)
5
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.
- [Django]-How to iterate through dictionary in a dictionary in django template?
- [Django]-Default filter in Django model
- [Django]-When to use Serializer's create() and ModelViewset's perform_create()
3
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>
- [Django]-Authenticate by IP address in Django
- [Django]-How do I get user IP address in Django?
- [Django]-"<Message: title>" needs to have a value for field "id" before this many-to-many relationship can be used.
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 .
- [Django]-AssertionError: database connection isn't set to UTC
- [Django]-How to get the current language in Django?
- [Django]-What is the difference between cached_property in Django vs. Python's functools?
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')
);
- [Django]-ImportError: Failed to import test module:
- [Django]-Python Django Rest Framework UnorderedObjectListWarning
- [Django]-How can I get tox and poetry to work together to support testing multiple versions of a Python dependency?
0
In my case, I forget to add this line to the index.js file
document.getElementById('root')
and I forget to import react-dom import ReactDOM from 'react-dom';
so you can use ReactDOM
later in the same file
Hope this will be helpful for you
- [Django]-Django β Rotating File Handler stuck when file is equal to maxBytes
- [Django]-Feedback on using Google App Engine?
- [Django]-Django TypeError: get() got multiple values for keyword argument 'invoice_id'