consider the following react code
the main.js file is:
import React from 'react'; import ReactDOM from 'react-dom'; import Maincontainner from './maincontainner'; ReactDOM.render( <div> <h1>News</h1> <Maincontainner/> </div>, document.getElementById('content') );
and the component is:
import React from 'react'; export default class Maincontainner extends React.Component{ render() { console.log("I am here"); return (<dev> Salman is here </dev>); } }
the problem is, when i run the application, i face with following error in my console:
Uncaught TypeError: _reactDom2.default.render is not a function
and here is the dependencies
"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" }
Update: 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 } };
I have also 1 .babelrc file
{ "presets": ["es2015", "react"] }
Use createRoot instead" occurs because the ReactDOM. render method has been deprecated. To solve the error, create a root element and use the ReactDOMClient.
import React from 'react'; import ReactDOM from 'react-dom'; import { createRoot } from 'react-dom/client'; import Switch from './components/Switch'; const root = ReactDOM. createRoot(document. getElementById('root')); root. render( <React.
createRoot(container[, options]); Create a React root for the supplied container and return the root. The root can be used to render a React element into the DOM with render : const root = createRoot(container); root.
You can change something in your Maincontainner
component.
import ReactDOM from 'react-dom';
render
to ReactDOM.render
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With