0

I am exploring react js routing but I got an error:

import React from 'react';Failed prop type: Invalid prop children supplied to Router.

[react-router] Location "/" did not match any routes

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 < /li> < li > About < /li > < li > Contact < /li></ul >
      { this.props.children } < /div>)
    }
}



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



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

Please let me know how to solve it.

2
  • There's nothing obviously wrong with how the Router is created. Can you post a more complete example demonstrating the issue? Commented Jan 16, 2017 at 15:43
  • I already add compleate code.Is there any version issue? Commented Jan 18, 2017 at 8:57

6 Answers 6

1

Using React Router v4

You need to import BrowserHistory from 'react router dom' if you want to use your links without '#' eg. www.yourexample.com/about.

You need to import Hashhistory from 'react router dom' if you want to use your links with '#' eg. www.yourexample.com/#/about.

Also if using react Router 4 you need to import {Switch} component from 'react router dom' to switch between links

Let me know if you needed full working example using react router 4

Sign up to request clarification or add additional context in comments.

Comments

0

You nee to import the react component

import React from 'react';

Comments

0

Here you can take a look at this.

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

class App extends Component {
   render() {
     return ( <div>
      <ul>
      <li> Home < /li> < li > About < /li > < li > Contact < /li></ul >
      { this.props.children } < /div>)
    }
}
class Home extends Component {
  render() {
    return ( < div >
    < h1 > Home... < /h1> < /div > )
  }
}



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

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

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

Comments

0

You Have to Include exact in your by default Route.

< Route exact path = "/" component = { App } >

Comments

0
You can use latest React Router which is react-router-dom and implement it in this way : 

//importing react-router-dom
import { BrowserRouter as Router, Route } from 'react-router-dom';

//make a separate router file and export the following const Route
//make sure you import Home and About components
const Routes = () => (
  <Router>
    <div>
      <Route exact path="/" component={Home} />
      <Route exact path="/about" component={About} />
    </div>
  </Router>
)

//then in your entry file, write code like this:

//import Routes
import Routes from './config/routes';

//then render Routes using ReactDom's render method
ReactDOM.render(<Routes />, document.getElementById('root'));

Comments

0

With react router 4, you need to wrap your routes inside a BrowserHistory or HashHistory component from ‘react-router-dom’.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.