[React Error]: Target container is not a DOM element

前端之家收集整理的这篇文章主要介绍了[React Error]: Target container is not a DOM element前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

webpack在后端对React组件进行渲染的时候,浏览器控制台报错:

compose.js:822 Uncaught Invariant Violation: _registerComponent(...): Target container is not a DOM element.

我很奇怪,webpack可以正常将我的React组件使用babel转换,我的组件代码如下:

import React from 'react'
import ReactDOM from 'react-dom'
import './compose.less'

class App extends React.Component {
  render() {
    return (
      <p className="compose-font">
        Little Bitch
      </p>
    )
  }
}
ReactDOM.render(<App />,document.getElementById('test-react-1'));

webpack.config.js

module.exports = {
  entry: './public/components/module_compose/compose.js',output: {
    path: path.resolve(__dirname,"./public/static_published/v1/js/"),filename: 'compose.js'
  },module: {
    loaders: [
      {test: /\.jsx?$/,loader: 'babel',exclude: /node_modules/},{test: /\.less$/,loader: 'style-loader!css-loader!less-loader'}
    ]
  }

出现此错误的原因是我将webpack生成的js文件放在了head,此时DOM还没有建立完毕,因此出现 not a DOM element 的错误,所以将js文件放在HTML底部就可以了。

原文链接:https://www.f2er.com/react/306317.html

猜你在找的React相关文章