Siavash
Siavash

Reputation: 3009

Routing problem in React-JS / react-router-dom

I installed the react-router on my project via run the following code snippet in the command prompt window:

npm install react-router

I found out that react-router installed successfully because there was no error on installing and also the output was:

Based on a tutorial, I set my Index.js like the following code:

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

Now when I want to compile/build my application, these error occurs:

./src/index.js
  Line 14:  'Router' is not defined          react/jsx-no-undef
  Line 14:  'browserHistory' is not defined  no-undef
  Line 15:  'Route' is not defined           react/jsx-no-undef
  Line 16:  'IndexRoute' is not defined      react/jsx-no-undef
  Line 17:  'Route' is not defined           react/jsx-no-undef
  Line 18:  'Route' is not defined           react/jsx-no-undef
  Line 19:  'Route' is not defined           react/jsx-no-undef

I know the compiler couldn't find the routing classes, and I have searched for problems like my issue on google and this community, but actually my search result was not helpful. Thank you for your responding.

Upvotes: 5

Views: 5202

Answers (1)

Hemadri Dasari
Hemadri Dasari

Reputation: 34014

You need to import Router, Route and in index.js file before you call them like

 import { Router, Route } from "react-router";

IndexRoute is not supported in React-Router v4 instead you can use Route with exact in place of IndexRoute like

  <Route exact path={path} component={Component} />

Edit:

browserHistory is not supported in react-router v4 so there is alternate solution for that check here https://github.com/ReactTraining/react-router/blob/25776d4dc89b8fb2f575884749766355992116b5/packages/react-router/docs/guides/migrating.md#the-router

Upvotes: 5

Related Questions