Answer the question
In order to leave comments, you need to log in
Console error "Target container is not a DOM element." when rendering React?
When rendering this JSX code, it gives an error in the console.
How can I fix it? I read that you need to add a script tag with a source to a js file in HTML, it did not help
Uncaught Error: Target container is not a DOM element.
at Object.render (react-dom.development.js:26086)
at eval (index.jsx:9)
at Object../src/index.jsx (bundle.js:19)
at __webpack_require__ (bundle.js:133)
at bundle.js:156
at bundle.js:158
import React from 'react';
import ReactDOM from 'react-dom';
let block = document.querySelector('#app')
let content = <h2>Send</h2>
console.log('hi')
ReactDOM.render(content, block)
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './src/index.jsx',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.m?jsx?$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react']
}
}
}
]
},
plugins: [new HtmlWebpackPlugin()],
devServer: {
contentBase: path.join(__dirname, 'dist'),
compress: true,
port: 9000,
},
}
{
"name": "first-react",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "webpack serve --mode development",
"build": "webpack --mode development"
},
"browserslist": "> 0.25%, not dead",
"repository": {
"type": "git",
"url": "git+https://github.com/Marat20/First-React.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/Marat20/First-React/issues"
},
"homepage": "https://github.com/Marat20/First-React#readme",
"devDependencies": {
"@babel/core": "^7.14.3",
"@babel/preset-env": "^7.14.2",
"@babel/preset-react": "^7.13.13",
"babel-loader": "^8.2.2",
"html-webpack-plugin": "^5.3.1",
"webpack": "^5.37.1",
"webpack-cli": "^4.7.0",
"webpack-dev-server": "^3.11.2"
},
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2"
}
}
Answer the question
In order to leave comments, you need to log in
let block = document.querySelector('#app')
In this line, you are trying to access an element with id=#app that you don't see.
Who will have a similar problem, pay attention in webpack.config to:
plugins: [
new HtmlWebpackPlugin({
title: './index.html'
})
],
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question