考虑以下反应代码

main.js文件是:

import React from 'react';
import ReactDOM from 'react-dom';
import Maincontainner from './maincontainner';

ReactDOM.render(
    <div>
        <h1>News</h1>
        <Maincontainner/>
    </div>,
    document.getElementById('content')
);

该组件是:
import React from 'react';


export default class  Maincontainner extends React.Component{

    render() {
        console.log("I am here");
        return (<dev> Salman is here </dev>);
    }
}

问题是,当我运行该应用程序时,我在控制台中遇到以下错误:
Uncaught TypeError: _reactDom2.default.render is not a function

这是依赖项
"dependencies": {
    "webpack": "^1.12.14",
    "webpack-dev-server": "^1.14.1",
    "react" : "^0.14.7",
    "react-dom" : "^0.14.7",
    "babel-cli": "^6.5.1",
    "babel-preset-es2015": "^6.5.0",
    "babel-loader": "^6.2.1",
    "babel-preset-react": "^6.3.13",
    "babelify": "^7.2.0"


  }

更新:webpack.config.json
module.exports={

    entry: './js/main.js',
    output:{

        filename: 'bundle.js'
    },
    module: {
        loaders: [
            {
                test: /.js?$/,
                loader: 'babel',
                exclude: /node_modules/,
                query: {
                    presets: ['es2015', 'react']
                }
            }
        ]
    },
    devServer:{


        port:3000
    }





};

我也有1个.babelrc文件
{
  "presets": ["es2015", "react"]
}

最佳答案

您可以在Maincontainner组件中进行更改。

  • 顶部添加:import ReactDOM from 'react-dom';
  • render更改为ReactDOM.render
  • 09-20 12:30