React, Uncaught ReferenceError: ReactDOM is not defined
Asked Answered
M

9

35

I am doing this Router tutorial.

My App.jsx file:

import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, Link, browserHistory, IndexRoute  } from 'react-router'

class App extends React.Component {
render() {
  return (
     <div>
        <ul>
           <li>Home</Link>
           <li>About</Link>
           <li>Contact</Link>
        </ul>

       {this.props.children}
     </div>
  )
 }
}

export default App;

class Home extends React.Component {
render() {
  return (
     <div>
        <h1>Home...</h1>
     </div>
  )
 }
}

export default Home;

class About extends React.Component {
render() {
  return (
     <div>
        <h1>About...</h1>
     </div>
  )
 }
}

export default About;

 class Contact extends React.Component {
render() {
  return (
     <div>
        <h1>Contact...</h1>
     </div>
  )
 }
}

export default Contact;

my Main.js file:

ReactDOM.render((
<Router history = {browserHistory}>
  <Route path = "/" component = {App}>
     <IndexRoute component = {Home} />
     <Route path = "home" component = {Home} />
     <Route path = "about" component = {About} />
     <Route path = "contact" component = {Contact} />
  </Route>
</Router>

), document.getElementById('app'))

This error is written to the console: index.js:

Uncaught ReferenceError: ReactDOM is not defined

I really dont know what to do. Followed every tut so far with no errors. Here I have no Idea what to do.

Meshwork answered 10/7, 2016 at 16:4 Comment(1)
You probably want to have a look at some info on ES6 modules.Rhizocarpous
B
32

You need to import ReactDOM in Main.js instead of App.jsx, as Main is where you are using ReactDOM to render.

Also need to import React in all files that use JSX.

Finally, also put react-router imports into Main, too.

The way imports work is, you import things you need, in places they're needed. It's not enough to import them once in one file and use in others.

Change Main.js to look like

import ReactDOM from 'react-dom'
import React from 'react'
import { Router, Route, browserHistory, IndexRoute  } from 'react-router'

ReactDOM.render((
<Router history = {browserHistory}>
  <Route path = "/" component = {App}>
     <IndexRoute component = {Home} />
     <Route path = "home" component = {Home} />
     <Route path = "about" component = {About} />
     <Route path = "contact" component = {Contact} />
  </Route>
</Router>

), document.getElementById('app'))
Beanpole answered 10/7, 2016 at 16:22 Comment(0)
F
5

This error also happens if there is a Typo
"import ReactDOM from "react-dom";"

and then call Reactdom.render( <App />, document.getElementById('root')) instead of ReactDOM.render....

Follicle answered 22/10, 2018 at 21:20 Comment(2)
For me it was ReactDOM and ReactDom, and the code editor highlighted both as if they were the same thing...Ithnan
I was doing something like import { ReactDOM } from 'react-dom'; and removing curly braces solves the problem: import ReactDOM from 'react-dom';Nabob
S
4

I had the same issue with my ReactDOM.render not to work until I imported the following:

import ReactDOM from 'react-dom'

into the page I created, which was the shopping cart page.

Shue answered 3/12, 2021 at 23:22 Comment(0)
A
2

1) install "npm install --save react-router-dom" with this command. 2) Know modify your App.jsx like this

import React from 'react';
import { Switch, Route, Link} from 'react-router-dom';
class App extends React.Component {
render() {
    return (
        <div>
            <Header/>
            <Content/>
        </div>
    );
}
}

class Header extends React.Component {
render() {
    return (
        <header>
            <nav>
                <ul>
                    <li><Link to='/'>Home</Link></li>
                    <li><Link to='/about'>About</Link></li>
                    <li><Link to='/contact'>Contact</Link></li>
                </ul>
            </nav>
        </header>
    );
}
}

class Content extends React.Component {
render() {
    return (
        <main>
            <Switch>
                <Route exact path='/' component={Home}/>
                <Route path='/about' component={About}/>
                <Route path='/contact' component={Contact}/>
            </Switch>
        </main>
    );
}
}
class Home extends React.Component {
render() {
    return (
        <div>
            <h1>Home...</h1>
        </div>
    );
}
}
class About extends React.Component {
render() {
    return (
        <div>
            <h1>About...</h1>
        </div>
    );
}
}
class Contact  extends React.Component {
render() {
    return (
        <div>
            <h1>Contact...</h1>
        </div>
    );
}
}

export default App;

4) modify your main.js like this

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.jsx';
import {HashRouter} from 'react-router-dom';
ReactDOM.render((
<HashRouter>
    <App />
</HashRouter>
), document.getElementById('app'))
Amey answered 17/5, 2017 at 8:5 Comment(0)
I
1

you should import ReactDOM and other stuff in Main.js

import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, browserHistory, IndexRoute  } from 'react-router'
import {App, Home, About,Contact} from './App'


ReactDOM.render((
<Router history = {browserHistory}>
  <Route path = "/" component = {App}>
     <IndexRoute component = {Home} />
     <Route path = "home" component = {Home} />
     <Route path = "about" component = {About} />
     <Route path = "contact" component = {Contact} />
  </Route>
</Router>

), document.getElementById('app'))

if App.js file contains all components you should change export statements:

from export default Component

to export Component.

And use named import in Main.js import {App, Home, About,Contact} from './App'

import React from 'react';
import { Link, browserHistory} from 'react-router'

class App extends React.Component {
render() {
  return (
     <div>
        <ul>
           <li>Home</Link>
           <li>About</Link>
           <li>Contact</Link>
        </ul>

       {this.props.children}
     </div>
  )
 }
}

export App;

class Home extends React.Component {
render() {
  return (
     <div>
        <h1>Home...</h1>
     </div>
  )
 }
}

export Home;

class About extends React.Component {
render() {
  return (
     <div>
        <h1>About...</h1>
     </div>
  )
 }
}

export About;

 class Contact extends React.Component {
render() {
  return (
     <div>
        <h1>Contact...</h1>
     </div>
  )
 }
}

export Contact;

for browserHistory, you must configure your server appropriately to serve at all routed paths. The simplier way is using hashHistory.

//import hashHistory
import { Router, Route, hashHistory, IndexRoute  } from 'react-router'
...
//pass in Router
<Router history = {hashHistory}> ....
Indent answered 10/7, 2016 at 16:22 Comment(0)
D
1

I just have a simple solution for it!

in my case the problem was with ReactDom namespace. just change it to something else and it work!

import XReactDom from 'react-dom'
Dissatisfaction answered 23/4, 2019 at 9:2 Comment(0)
A
0

In this tutor this command "npm install react-router" does not save it in package.json file so modify and run command to "npm install --save react-router".

Amey answered 16/5, 2017 at 13:31 Comment(0)
S
0

RouterDOM is used in main file, in your case its Main.js (where rendering takes place). Your Main.js file is the starting point and as its render reactDOM but cannot find the import for it. Just to import it in same file where its used.

Stifling answered 31/12, 2021 at 17:18 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Bilbo
H
0

I had a similar issue and was able to fix it by importing ReactDOM like this:

import * as ReactDOM from 'react-dom';

And then used it like this:

 <Fragment>
      {ReactDOM.createPortal(
        <Backdrop onConfirm={props.onConfirm} />,
        document.getElementById("backdrop-root")
      )}
 </Fragment>
Heterocercal answered 11/8, 2022 at 0:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.