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
82
votes
16 answers

Netlify renders 404 on page refresh (using React and react-router)

I have deployed my site with Netlify, and I’m having trouble with the routing. Here is my website: https://redux-co.netlify.com/ And my GitHub repo: https://github.com/jenna-m/redux-co Specifically, if a user navigates to any page besides the home…
Jenna
  • 1,159
  • 2
  • 10
  • 16
81
votes
11 answers

Invariant Violation: You should not use outside a

I have a problem that I don't know how to solve, I get this error when running npm test Invariant Violation: You should not use outside a What can the problem be and how can I solve it? The test I run is the standard app.test.js…
Fille_M
  • 873
  • 1
  • 6
  • 7
81
votes
6 answers

react router v4 default page(not found page)

This is common purpose, directing unmatch request to notfound page. making this with react-router v4 looks like previous versions and I expect this sample works below. Links work fine but I expect NotFound component called only unknown url…
TyForHelpDude
  • 4,828
  • 10
  • 48
  • 96
81
votes
10 answers

React router redirect after action redux

I'm using react-redux and react-router. I need to redirect after an action is dispatched. For example: I have registration a few steps. And after action: function registerStep1Success(object) { return { type: REGISTER_STEP1_SUCCESS, …
ximet
  • 1,106
  • 2
  • 9
  • 15
80
votes
22 answers

How do I reload a page with react-router?

I can see in this file (https://github.com/ReactTraining/react-router/blob/v0.13.3/modules/createRouter.js) that there is a refresh function but I have no idea how to call it. I'm fairly new to react-router, I've only used it to move between some…
Sheriff
  • 937
  • 1
  • 7
  • 10
80
votes
3 answers

What is the trade off between history push and replace?

I use History package to redirect invalid URL to 404 page on my react component. I use .replace instead of .push simply because I don't want the browser to record any invalid url I have visited. Have I missed the point here? And is there a trade-off…
Kris MP
  • 2,305
  • 6
  • 26
  • 38
78
votes
16 answers

react-router-dom useParams() inside class component

I'm trying to load a details view based on a react-router-dom route that should grab the URL parameter (id) and use that to further populate the component. My route looks like /task/:id and my component loads fine, until I try to grab the :id from…
Jorre
  • 17,273
  • 32
  • 100
  • 145
78
votes
17 answers

Forcing a React-Router to load a page, even if we're already on that page

Is there a way to force a React-Router to load a page from path, even when the current location is already that page? I can't seem to find any mention of this in the react-router documentations. We have a page on a route for "apply" that…
Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
78
votes
2 answers

How to sync Redux state and url hash tag params

We have a list of lectures and chapters where the user can select and deselect them. The two lists are stored in a redux store. Now we want to keep a representation of selected lecture slugs and chapter slugs in the hash tag of the url and any…
JoKer
  • 1,074
  • 1
  • 8
  • 11
78
votes
7 answers

Using React-Router with a layout page or multiple components per page

I am adding react router to an existing project. At present a model is passed in to a root component which contains a navigation component for the sub navigation and a main component. The examples of react router I've found only have one child…
Tom
  • 12,591
  • 13
  • 72
  • 112
77
votes
3 answers

react-router: How do I update the url without causing a navigation/reload?

With react router I have done this up update the url: this.props.history.push({ pathname: `/product/${this.props.product.id}`, }); However this causes a re-render/navigation change. Is there a way to update the url without doing…
strangeQuirks
  • 4,761
  • 9
  • 40
  • 67
77
votes
2 answers

TypeError: scrollIntoView is not a function

I am new to react-testing-library / jest and attempting to write a test to see if the navigation of routes (using react-router-dom) is performed correctly. So far I have been following the README and this tutorial on how to use. One of my…
Charklewis
  • 4,427
  • 4
  • 31
  • 69
76
votes
5 answers

Next.js: Router.push with state

I'm using next.js for rebuilding an app for server side rendering. I have a button that handles a search request. In the old app, the handler was this one: search = (event) => { event.preventDefault(); history.push({ pathname:…
DaFunkyAlex
  • 1,859
  • 2
  • 24
  • 36
76
votes
11 answers

react-router: How to disable a , if its active?

How can I disable a in react-router, if its URL already active? E.g. if my URL wouldn't change on a click on I want to prevent clicking at all or render a instead of a . The only solution which comes to my mind is using…
Pipo
  • 5,623
  • 7
  • 36
  • 46
75
votes
10 answers

React-Router only one child

I keep on getting the error: A 'Router' may have only one child element when using react-router. I can't seem to figure out why this is not working, since it's exactly like the code they show in their example: Quick Start Here is my code: import…
Mar
  • 1,129
  • 1
  • 13
  • 18