Questions tagged [react-router]

React Router - A complete routing library for React inspired by Ember's routing system

Introduction

React Router is a powerful routing library built on top of React that helps you add new screens and flows to your application incredibly quickly, all while keeping the URL in sync with what's being displayed on the page.

Example

import React from 'react'
import ReactDOM from 'react-dom'
import {
    BrowserRouter as Router,
    Route,
    Link
} from 'react-router-dom'

const BasicExample = () => (
    <Router>
        <div>
            <ul>
                <li><Link to="/">Home</Link></li>
                <li><Link to="/about">About</Link></li>
                <li><Link to="/topics">Topics</Link></li>
            </ul>
            <hr/>

            <Route exact path="/" component={Home}/>
            <Route path="/about" component={About}/>
            <Route path="/topics" component={Topics}/>
        </div>
    </Router>
);

const Home = () => (
    <div>
        <h2>Home</h2>
    </div>
);

const About = () => (
    <div>
        <h2>About</h2>
    </div>
);

const Topics = ({ match }) => (
    <div>
        <h2>Topics</h2>
        <ul>
            <li>
                <Link to={`${match.url}/rendering`}>
                    Rendering with React
                </Link>
            </li>
            <li>
                <Link to={`${match.url}/components`}>
                    Components
                </Link>
            </li>
            <li>
                <Link to={`${match.url}/props-v-state`}>
                    Props v. State
                </Link>
            </li>
        </ul>

        <Route path={`${match.url}/:topicId`} component={Topic}/>
        <Route exact path={match.url} render={() => (
            <h3>Please select a topic.</h3>
        )}/>
    </div>
);

const Topic = ({ match }) => (
    <div>
        <h3>{match.params.topicId}</h3>
    </div>
);

ReactDOM.render(<BasicExample />, document.body)

Docs


Related tags

20178 questions
55
votes
4 answers

How to mock history.push with the new React Router Hooks using Jest

I am trying to mock history.push inside the new useHistory hook on react-router and using @testing-library/react. I just mocked the module like the first answer here: How to test components using new react router hooks? So I am…
55
votes
7 answers

You should not use Route or withRouter() outside a Router when using react-router 4 and styled-component in react

I'm trying to build my first portfolio website and got stuck in routing using react-router-dom 4.2.2 and styled-components 2.2.3. error message: You should not use Route or withRouter() outside a Router I also try using Link instead of NavLink but…
JongHun Lee
  • 551
  • 1
  • 5
  • 4
55
votes
5 answers

Using React IndexRoute in react-router v4

I'm learning React myself with online tutorial. So this is a basic example about using React Router:
Quoc-Hao Tran
  • 1,312
  • 3
  • 13
  • 20
55
votes
4 answers

Changing the URL in react-router v4 without using Redirect or Link

I'm using react-router v4 and material-ui in my React app. I was wondering how to change the URL once there is a click on a GridTile within GridList. My initial idea was to use a handler for onTouchTap. However, the only way I can see to redirect is…
Alex
  • 1,021
  • 3
  • 10
  • 16
55
votes
3 answers

How to fetch the new data in response to React Router change with Redux?

I'm using Redux, redux-router and reactjs. I'm trying to make an app where I fetch information on route change, so, I've something like:
Franco Risso
  • 1,572
  • 2
  • 13
  • 18
54
votes
9 answers

Programmatically Navigate using react-router

I am developing an application in which I check if the user is not loggedIn. I have to display the login form, else dispatch an action that would change the route and load other component. Here is my code: render() { if (isLoggedIn) { //…
Gaurav Mehta
  • 603
  • 1
  • 6
  • 10
53
votes
13 answers

Material-ui adding Link component from react-router

I'm struggling to add component to my material-ui AppBar This is my navigation class: class Navigation extends Component { constructor(props) { super(props) } render() { var styles = { appBar: { flexWrap: 'wrap' …
Evaldas Buinauskas
  • 13,739
  • 11
  • 55
  • 107
53
votes
4 answers

What is the difference between hashHistory and browserHistory in react router?

I have googled quite some bit, but I didn't find a clear answer to the following question: What is the difference between hashHistory and browserHistory in react-router?
Ben Bieler
  • 1,518
  • 1
  • 14
  • 22
52
votes
2 answers

React router Switch behavior

(react-router-dom version: 4.1.1) I have working routes set up, but I'm a bit confused about why the was necessary: index.js import React from 'react'; import { HashRouter, Route, Switch } from 'react-router-dom'; import App from…
Jess
  • 1,515
  • 3
  • 23
  • 32
52
votes
15 answers

React-router v4 this.props.history.push(...) not working

I'm trying to route programatically using this.props.history.push(..) but it doesn't seem to work. Here's the router: import { BrowserRouter as Router, Route } from 'react-router-dom';
Andrea Ongaro
  • 521
  • 1
  • 4
  • 3
51
votes
5 answers

React Routing works in local machine but not Heroku

React/react router/heroku question here (it is probably heroku where it is failing). I am following this wonderful tutorial: https://medium.com/@patriciolpezjuri/using-create-react-app-with-react-router-express-js-8fa658bf892d#.y77yjte2j and…
swyx
  • 2,378
  • 5
  • 24
  • 39
51
votes
2 answers

React-Router - Uncaught TypeError: Cannot read property 'getCurrentLocation' of undefined

I'm using the latest version of react-router (version ^3.0.0). I wrote the following routing using ES5: routes.js: var React = require("react"); var Router = require("react-router"); var AuthorPage = require('./components/authors/authorPage') var…
SyndicatorBBB
  • 1,757
  • 2
  • 26
  • 44
51
votes
4 answers

How to let react router respond with 404 status code?

I'm using react router as root and all requests under "/" are directed to react router. And when react router found that the url is not matched with any of the defined components, it renders with NoMatch component. And here goes the problem, NoMatch…
Liang
  • 1,127
  • 2
  • 11
  • 22
51
votes
8 answers

Automatic redirect after login with react-router

I wanted to build a Facebook login into my react/react-router/flux application. I have a listener registered on the login event and would like to redirect the user to '/dashboard' if they are logged in. How can I do that? location.push didn't work…
Daniel Schmidt
  • 11,605
  • 5
  • 38
  • 70
50
votes
12 answers

React Router v4 with multiple layouts

I'd like to render some of my routes within my public layout, and some other routes within my private layout, is there a clean way to do this? Example that obviously doesn't work, but I hope explains roughly what I'm looking for:
Florian Bienefelt
  • 1,448
  • 5
  • 15
  • 28