Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 917
Siavash Avatar asked Oct 12 '18 04:10

Siavash


People also ask

What problem does React router solve?

In React, routers help create and navigate between the different URLs that make up your web application. They allow your user to move between the components of your app while preserving user state, and can provide unique URLs for these components to make them more shareable.

What is routes in React router Dom?

Routing is a process in which a user is directed to different pages based on their action or request. ReactJS Router is mainly used for developing Single Page Web Applications. React Router is used to define multiple routes in the application.

Why use React router DOM instead of React router?

The primary difference between them lies in their usage. React Router DOM is for web applications and React Router Native is for mobile applications made with React Native.


1 Answers

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

like image 126
Hemadri Dasari Avatar answered Sep 23 '22 05:09

Hemadri Dasari