Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is react router throwing 'Cannot GET /example' error?

I'm learning React, and I need to add some routes with React Routes.

I have installed react-router with npm install react-router.

This is the main js where I have to declare the routes

import React from 'react';
import {ReactDOM, render} from 'react-dom';
import App from './Components/AppComponent';
import Example from './Components/ExampleComponent';
import { Router, Route, IndexRoute, Link, IndexLink, browserHistory } from 'react-router'


render((
    <Router history={browserHistory}>
        <Route path="/" component={App}>
            <Route path="example" component={Example}/>
        </Route>
    </Router>
), document.getElementById('App'));

Navigating to / works fine. But when I go to /example I'm getting:

Cannot GET /example

error

What am I doing wrong?

This way I have the same issue:

<Router>
    <Route path="/" component={App} />
    <Route path="/example" component={Example}/>
</Router>

Example Component

import React from 'react';

class Example extends React.Component {
    render(){
        return <div>Example</div>
    }
}

export default Example

This is the Link:

<Link to={'/example'}>aa</Link>

There are no errors in console.

After the change I'm getting this errors in console (Caused by the Link)

index.js (line 27585) Warning: [react-router] Location "/" did not match any routes

index.js (line 27585) Warning: [react-router] Router no longer defaults the history prop to hash history. Please use the hashHistory singleton instead. https://github.com/ReactTraining/react-router/blob/master/upgrade-guides/v2.0.0.md#no-default-history

like image 931
Pablo Avatar asked Jan 30 '16 17:01

Pablo


People also ask

What is react router with example?

React Router is a standard library for routing in React. It enables the navigation among views of various components in a React Application, allows changing the browser URL, and keeps the UI in sync with the URL. Let us create a simple application to React to understand how the React Router works.

What is difference between BrowserRouter and HashRouter?

Simply, BrowserRouter syncs the UI with the current URL in your browser, This is done by the means of HTML-5 History API. On the other hand, HashRouter uses the Hash part of your URL to sync. Save this answer.

Is react 18 compatible with React 5 router?

1. react-scripts 5.0. 1 improves compatibility with React 18. If you were using react-scripts 4 before, you might see some issues from webpack5, since react-scripts 5 uses webpack5.


1 Answers

Your example route is nested within your parent route so you should't need a / in /example.

Also you can try setting an IndexRoute with whatever your home component would be so that it has a reference.

Linking to the route would probably help so you can see what is coming up in the URL to make sure its what your looking for.

Your App component shouldnt really be rendering anything other than {this.props.children} because that is where all the components that the router is handling will be rendered, the router doesn't just render things from the main file it controls the routes and which components to mount in the App component(Usually called an AppController) so you need to build a Home component that will render at the / route and declare that using IndexRoute in your router then set Links between the components and if everything is done properly than you should be good to go.

Link:

<Link to="example">
  Click Me!
</Link>

Main:

import React from 'react';
import {ReactDOM, render} from 'react-dom';
import App from './Components/AppComponent';
import Home from './Components/Home';
import Example from './Components/ExampleComponent';
import { Router, Route, IndexRoute, Link, IndexLink, browserHistory } from 'react-router'


ReactDOM.render((
    <Router history={browserHistory}>
        <Route path="/" component={App}>
            <IndexRoute component={Home}>
            <Route path="example" component={Example}/>
        </Route>
    </Router>
), document.getElementById('App'));

AppController:

import React from 'react';

class App extends React.Component {

  render() {
    return (
      <div>
        <div className="routerView">
          {this.props.children}
        </div>
      </div>
    );
  }
}

export default App;

Example Component:

import React from 'react';
import {Link} from 'react-router';


class Example extends React.Component {
    render(){
        return(
          <div>
              Example
              <Link to="/">Click Me!</Link>
          </div>
        )
    }
}

export default Example

Home component:

import React from 'react';
import {Link} from 'react-router';

class Home extends React.Component {
    render(){
        return (
            <div>
               Home
               <Link to="/example">To Example!</Link>
            </div>
        )
    }
}

export default Home
like image 56
RichG Avatar answered Oct 18 '22 09:10

RichG