React Router v4 routes not working

react-router not loading component
react router not rendering component
react-router nested routes not working
react-router-dom not working
react-router not working after build
react router not working on any route other than "/"
react-router exact not working
react-router not working in production

I am relatively new to reacts and I'm trying to figure out how to get React router to work. I've got a super simple test app that looks like this:

import React from 'react';
import ReactDOM from 'react-dom';

import {BrowserRouter as Router, Route, Switch, IndexRoute, Link} from 'react-router-dom';

const Home = () => <h1><Link to= "/about">Click Me</Link></h1>
const About = () => <h1>About Us</h1>

const Test = () => (
  <Router>
    <Switch>
    <Route path ="/" component = {Home} />
    <Route path ="/about" component = {About} />
    </Switch>
  </Router>
)

ReactDOM.render(<Test />, document.getElementById('app'));

when I run the app the home component loads without any trouble, and when I click the "Click Me" link the url changes to localhost/about, however nothing happens. If I click refresh I get a "Cannot GET /about." Clearly I am doing something wrong but I haven't been able to figure out what. I am also using Webpack.

You need to use an exact path for / otherwise it will also match /about.

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

As mentioned in the comments, for something this simple I would suggest using Create React App which will make sure your server code and your webpack settings are all correct. Once you use create-react-app you'll just need to use npm to install the react router v4 package, and then put your code above into the App.js file and it should work. There are some small changes to your code to get it to work with create-react-app as can be seen below:

// App.js
import React from 'react';

import { BrowserRouter as Router, Route, Switch, Link } from 'react-router-dom';

const Home = () => <h1><Link to="/about">Click Me</Link></h1>
const About = () => <h1>About Us</h1>

const App = () => (
  <Router>
    <Switch>
      <Route exact path="/" component={Home} />
      <Route path="/about" component={About} />
    </Switch>
  </Router>
)

export default App;

The quick start instructions from React Router V4 documentation will tell you pretty much the same as I just explained.

React-router-dom v4 nested routes not working, This is one way of doing what you described (note there are other ways of handling layouts directly in your React Components). In order to keep the example� Yes generally React Router and Redux work fine together. Occasionally though, an app can have a component that doesn't update when the location changes (child routes or active nav links don't update). This is called Blocked Updates.

If you're running from localhost you need to add historyApiFallback: true to your webpack.config file

Routing not working properly � Issue #5906 � ReactTraining/react , I am trying to setup react router in my react application. Routing not working properly #5906. Closed everything. I am new to this router v4. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

Maybe it can help someone. I forgot to use the proper history, instead of using the Router I was using the BrowserRouter which ignores the property history={...} to use its own. I was trying to redirect the page with my manually created history but BrowserRouter was using its own one.

Basic example of React Router: BrowserRouter, Link, Route, and , Route is the conditionally shown component based on matching a path to a URL. For react-router-dom@4.1.2, this did not work correctly for me until I changed .com/questions/42123261/programmatically-navigate-using-react-router-v4. Before we even go about creating our protected routes, we’ll need a way to figure out if the user is authenticated. Because this is a tutorial about React Router v4 protected routes and not about authentication, we’ll use a dummy object to mock our auth service.

if Adding exact Don't resolve issue, i have replaced this with

  <Route exect path="/" component={App} />
  <Route path="/login" component={Login} />
  <Route path="/register" component={Register} />

This one

  <Route exact path="/"> <App /> </Route>
  <Route path="/login"> <Login /> </Route>
  <Route path="/register"> <Register /> </Route>

putting component as Route Child solves my page routing i hope it helps someone else too

React Router: Declarative Routing for React.js, React Training. Learn once, Route anywhere Your browser does not currently recognize any of the video formats available. Click here to visit our frequently� How To Connect Two Routers On One Home Network Using A Lan Cable Stock Router Netgear/TP-Link - Duration: 33:19. Richard Lloyd 2,879,692 views

Server Rendering, We pass in the requested url from the server so the routes can match and a context client <BrowserRouter> <App/> </BrowserRouter> // server (not the complete story) to assist with data loading and server rendering with static route configs. Don't get me wrong, I believe that what you're doing w/ react-router is great, and I don't mean this to become yet another critique to the devs, as @South-Paw has voiced most of my frustrations already. Once there's enough community support with react-router v4, I'll be sure to come back and finally master React once and for all.

How To Set Up Conditional and Responsive Routing with React , import React from "react"; import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom"; export default function App() { return ( <Router> <div>� That said, to handle juggling both routes that require authentication, and those that require the user to be unauthenticated, I added two new components that act as wrappers for React Router’s Route component.

How to Utilize React Router in ReactJS , Responsive routing in React involves serving different routes to users How To Set Up Conditional and Responsive Routing with React Router v4 either showing or not showing different elements by using the CSS props. React Router v4 introduced a new declarative, component-based approach to routing. With that approach came some confusion around nested routes. In this post, we’ll break down that confusion and you’ll learn how to nest routes with React Router.

Comments
  • Are there any messages in the console? It may be an issue with the About component and not the router itself. Also, try to put the About component on the / path to see what happens.
  • RE: Cannot GET /about: Consider using create-react-app. It's facebook blessed and makes the server part of this work out of the box in development, among many other development niceties. Without it you need to manage routing everything to index.html yourself, or use the hash router in react-router.
  • Um excuse me but I'm having a similar issue although I've added exact and its still not working
  • @akshaykishore You should open a new question and give us the code you are using that will reproduce your problem.
  • The code is similar except I'm importing the components from an another file. By the way on refreshing the second page component is rendered .
  • This was my issue!! Thank you.
  • A bit more info around this would be really helpful!
  • how do you implement this if usign create-react-app? and is it an issue if your stack is deployed to a public server? I get the same issues both local and on my server
  • This needs to be added to docs, I would have never known this. Been pulling my hair out over this.
  • i dont have a webpack.config file. What should i do?