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
184
votes
11 answers

How can I redirect in React Router v6?

I am trying to upgrade to React Router v6 (react-router-dom 6.0.1). Here is my updated code: import { BrowserRouter, Navigate, Route, Routes } from 'react-router-dom'; } />
Hongbo Miao
  • 45,290
  • 60
  • 174
  • 267
180
votes
6 answers

What is withRouter for in react-router-dom?

I've sometimes seen people wrap their components in withRouter when they are exporting them: import { withRouter } from 'react-router-dom'; class Foo extends React.Component { // ... } export default withRouter(Foo); What is this for, and when…
LondonRob
  • 73,083
  • 37
  • 144
  • 201
175
votes
8 answers

Matched leaf route at location "/" does not have an element

Matched leaf route at location "/" does not have an element. This means it will render an with a null value by default resulting in an "empty" page //App.js File import { BrowserRouter as Router, Routes, Route } from 'react-router-dom'; import…
user16102215
  • 1,751
  • 2
  • 5
  • 4
173
votes
18 answers

Next.js Redirect from / to another page

I'm new in Next.js and I'm wondering how to redirect from start page ( / ) to /hello-nextjs for example. Once user loads a page and after that determine if path === / redirect to /hello-nextjs In react-router we do something like:
Arthur
  • 3,056
  • 7
  • 31
  • 61
172
votes
9 answers

How do I avoid 'Function components cannot be given refs' when using react-router-dom?

I have the following (using Material UI).... import React from "react"; import { NavLink } from "react-router-dom"; import Tabs from "@material-ui/core/Tabs"; import Tab from "@material-ui/core/Tab"; function LinkTab(link){ return
JGleason
  • 3,067
  • 6
  • 20
  • 54
172
votes
5 answers

What is the best way to redirect a page using React Router?

I am new to React Router and learn that there are so many ways to redirect a page: Using browserHistory.push("/path") import { browserHistory } from 'react-router'; //do something... browserHistory.push("/path"); Using…
Liutong Chen
  • 2,915
  • 5
  • 22
  • 29
169
votes
16 answers

React-Router open Link in new tab

Is there a way to get React Router to open a link in new tab? I tried this and it did not work. Test It's possible to fluff it by adding something like onClick="foo" to the…
mcd
  • 6,446
  • 9
  • 27
  • 32
163
votes
22 answers

How to implement authenticated routes in React Router 4?

I was trying to implement authenticated routes but found that React Router 4 now prevents this from working:
Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
155
votes
32 answers

Getting "Cannot call a class as a function" in my React Project

I'm trying to add a React map component to my project but run into an error. I'm using Fullstack React's blog post as a reference. I tracked down where the error gets thrown in google_map.js line 83: function _classCallCheck(instance, Constructor) {…
Mike Fleming
  • 2,593
  • 4
  • 14
  • 24
154
votes
3 answers

HtmlWebpackPlugin injects relative path files which breaks when loading non-root website paths

I am using webpack and the HtmlWebpackPlugin to inject bundled js and css into an html template file. new HtmlWebpackPlugin({ template: 'client/index.tpl.html', inject: 'body', filename: 'index.html' }), And it produces the following html…
aherriot
  • 4,495
  • 6
  • 24
  • 36
147
votes
37 answers

React router changes url but not view

I am having trouble changing the view in react with routing. I only want to show a list of users, and clicking on each user should navigate to a details page. Here is the router: import React from "react"; import ReactDOM from "react-dom"; import {…
MARyan87
  • 1,656
  • 2
  • 12
  • 15
144
votes
15 answers

React Router Pass Param to Component

const rootEl = document.getElementById('root'); ReactDOM.render(
M T
  • 4,099
  • 4
  • 21
  • 27
139
votes
19 answers
137
votes
7 answers

React Router v4 vs benefits

Besides the ability to set an "activeClassName" and "activeStyle" on NavLink, is there any reason to use NavLink over Link when creating links to other routes on non-navigational elements (ie. not main nav in header or footer) on your site that…
yam55
  • 1,541
  • 2
  • 11
  • 12
136
votes
7 answers

react router v^4.0.0 Uncaught TypeError: Cannot read property 'location' of undefined

I've been having some trouble with react router (i'm using version^4.0.0). this is my index.js import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; import './index.css'; import { Router, Route, Link, browserHistory }…
Lucas fersa
  • 1,467
  • 2
  • 10
  • 8