React,Uncaught ReferenceError:未定义ReactDOM

时间:2016-07-10 16:04:49

标签: javascript reactjs react-router web-component

我在做this Router tutorial.

我的App.jsx文件:

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;

我的Main.js文件:

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'))

此错误将写入控制台:index.js:

  

未捕获的ReferenceError:未定义ReactDOM

我真的不知道该怎么做。到目前为止,每个tut都没有错误。在这里,我不知道该怎么做。

6 个答案:

答案 0 :(得分:10)

您需要在ReactDOM而不是Main.js中导入App.jsx,因为Main是您使用ReactDOM进行渲染的地方。

还需要在使用JSX的所有文件中导入React

最后,同样将react-router导入Main

导入工作的方式是,您导入所需的内容,在他们需要的地方。仅在一个文件中导入一次并在其他文件中使用它是不够的。

Main.js更改为

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'))

答案 1 :(得分:2)

1)使用此命令安装“npm install --save react-router-dom”。 2)知道像这样修改你的App.jsx

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)像这样修改你的main.js

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'))

答案 2 :(得分:1)

您应该在Main.js

中导入ReactDOM和其他内容
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'))

如果App.js文件包含所有组件,则应更改导出语句:

来自 export default Component

export Component

在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;

对于browserHistory,您必须将服务器appropriately配置为在所有路由路径上提供服务。更简单的方法是使用hashHistory。

//import hashHistory
import { Router, Route, hashHistory, IndexRoute  } from 'react-router'
...
//pass in Router
<Router history = {hashHistory}> ....

答案 3 :(得分:1)

如果有错字,也会发生此错误
"import ReactDOM from "react-dom";"

,然后调用Reactdom.render( <App />, document.getElementById('root'))而不是ReactDOM.render....

答案 4 :(得分:1)

我只是有一个简单的解决方案!

在我的情况下,问题出在ReactDom名称空间。只需将其更改为其他内容即可使用!

import XReactDom from 'react-dom'

答案 5 :(得分:0)

在本教程中,此命令“npm install react-router”不会将其保存在package.json文件中,因此修改并运行命令为“npm install --save react-router”。