you should import ReactDOM and other materials into 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 the App.js file contains all the components , you must change the export instructions:
from export default Component
before 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 the history browser, you must configure your server accordingly to serve on all routed routes. An easier way is to use hashHistory.
//import hashHistory import { Router, Route, hashHistory, IndexRoute } from 'react-router' ... //pass in Router <Router history = {hashHistory}> ....
source share