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
134
votes
8 answers

How to go back to previous route in react-router-dom v6

On early versions we can go back to previous route using history. history.goBack() How I can achieve that with v6 of react-router-dom?
Hamza Khursheed
  • 2,399
  • 2
  • 15
  • 17
133
votes
10 answers

Getting query parameters from react-router hash fragment

I'm using react and react-router for my application on the client side. I can't seem to figure out how to get the following query parameters from a url like: http://xmen.database/search#/?status=APPROVED&page=1&limit=20 My routes look like this…
129
votes
7 answers

How to use onClick event on react Link component?

I am using the Link component from the reactjs router and I cannot get the onClickevent working. This is the code: Here Is this the way to do it or another way?
bier hier
  • 20,970
  • 42
  • 97
  • 166
127
votes
3 answers

What does ...rest mean in React JSX?

Looking at this React Router Dom v4 example https://reacttraining.com/react-router/web/example/auth-workflow I see that PrivateRoute component destructures a rest prop like this const PrivateRoute = ({ component: Component, ...rest }) => (
razor7
  • 2,165
  • 2
  • 19
  • 32
125
votes
7 answers

How to get history on react-router v4?

I having some little issue migrating from React-Router v3 to v4. in v3 I was able to do this anywhere: import { browserHistory } from 'react-router'; browserHistory.push('/some/path'); How do I achieve this in v4. I know that I could use, the hoc…
storm_buster
  • 7,362
  • 18
  • 53
  • 75
125
votes
10 answers

React router, pass data when navigating programmatically?

We could navigate to different path using this.props.router.push('/some/path') Is there a way to send params (object) along when navigating? There are other options I can think of, but wonder if passing object is possible at all? I could embed id…
eugene
  • 39,839
  • 68
  • 255
  • 489
124
votes
14 answers

Component does not remount when route parameters change

I'm working on a react application using react-router. I have a project page that has a url as follows: myapplication.com/project/unique-project-id When the project component loads, I trigger a data request for that project from the…
Constellates
  • 2,083
  • 3
  • 19
  • 29
122
votes
16 answers

You should not use outside a

I'm trying to set up react-router in an example application, and I'm getting the following error: You should not use outside a My app is set up like so: Parent component const router = (
a7dc
  • 3,323
  • 7
  • 32
  • 50
121
votes
6 answers

Property 'exact' does not exist on type

I am trying to use react-router-dom inside my react app and also I am using typescript instead of javascript. The issue here is that I can't import Route inside my component and make it work. I already installed @types/react-router-dom but for some…
Veselin Kontić
  • 1,638
  • 2
  • 11
  • 23
120
votes
15 answers

How to set up Google Analytics for React-Router?

I'm trying set up Google Analytics on my react site, and have come across a few packages, but none of which has the kind of set up that I have in terms of examples. Was hoping someone could shed some light on this. The package I'm looking at is,…
John Fu
  • 1,812
  • 2
  • 15
  • 20
120
votes
9 answers

How to allow for webpack-dev-server to allow entry points from react-router

I'm creating an app that uses webpack-dev-server in development alongside react-router. It seems that webpack-dev-server is built around the assumption that you will have a public entry point at one place (i.e. "/"), whereas react-router allows for…
Nathan Wienert
  • 1,623
  • 3
  • 19
  • 25
119
votes
7 answers

Active link with React-Router?

I'm trying out React-Router (v4) and I'm having issues starting off the Nav to have one of the Link's be active. If I click on any of the Link tags, then the active stuff starts working. However, I'd like for Home Link to be active as soon as the…
Saad
  • 49,729
  • 21
  • 73
  • 112
116
votes
14 answers

Put length constraint in a TextField in react js

I am taking an input from the user of the card number and wants that the length entered by user must not be less than and more than 12. Here is the declaration of my textfield.
Mayank Bansal
  • 1,755
  • 2
  • 9
  • 22
116
votes
7 answers

How to refresh a Page using react-route Link

I am trying to refresh a page using react-route Link. But the way I have implemented it goes to the URL one step back.(as an example if the URL was ../client/home/register and when I press the reload is goes to ../client/home) below is my…
Anna
  • 1,499
  • 4
  • 13
  • 18
116
votes
2 answers

Client Routing (using react-router) and Server-Side Routing

I have been thinking and I am confused with the routing between Client and Server. Suppose I use ReactJS for server-side rendering before sending the request back to web browser, and use react-router as a client-side routing to switch between pages…
heartmon
  • 1,411
  • 2
  • 13
  • 14