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
66
votes
3 answers

BrowserRouter vs Router with history.push()

I am trying to understand the difference between BrowserRouter and Router of the react-router-dom (v5) package and what difference it makes for my example below. The documentation says: BrowserRouter A that uses the HTML5 history API…
mitchkman
  • 6,201
  • 8
  • 39
  • 67
66
votes
5 answers

Nested Routes in React Router v4

I'm trying to set up some nested routes to add a common layout. Check the code out:
Sean Gillespie
  • 761
  • 1
  • 5
  • 6
66
votes
6 answers

how to navigate from one page to another in react js?

I have two components. In first component I have one button. On click of button I want to navigate to another component or another page. here is my code http://codepen.io/naveennsit/pen/pymqPa?editors=1010 class App extends React.Component { …
user5711656
  • 3,310
  • 4
  • 33
  • 70
66
votes
3 answers

React Router Authorization

What are the best practices for authorization checking prior to a component mounting? I use react-router 1.x Here are my routes React.render((
theo
  • 704
  • 1
  • 5
  • 9
65
votes
19 answers

Refresh previous screen on goBack()

I am new to React Native. How can we refresh/reload previous screen when returning to it by calling goBack()? Lets say we have 3 screens A, B, C: A -> B -> C When we run goBack() from screen C it goes back to screen B but with old state/data. How…
64
votes
7 answers

react-router-dom with TypeScript

I'm trying to use react router with TypeScript. However, I have certain problems using withRouter function. On the last line, I'm getting pretty weird error: Argument of type 'ComponentClass<{}>' is not assignable to parameter of type…
64
votes
7 answers

Uncaught TypeError: Cannot read property 'push' of undefined (React-Router-Dom)

I have a Dashboard with rotating slides, each of which has a corresponding tab in Bldgs. Both Dashboard.js and Bldgs.js are children to my App.js. When a user clicks on a specific slide A in Dashboard.js, Dashboard needs to tell App.js so that App…
Mike
  • 1,307
  • 3
  • 17
  • 29
63
votes
6 answers

React wrapper: React does not recognize the `staticContext` prop on a DOM element

I'm trying to create a wrapper component around the react-router-dom NavLink component. I would like my custom component to accept all of NavLinks props, and proxy them down to NavLink. However when I do this, I'm getting: Warning: React does not…
Nicolas Widart
  • 1,187
  • 4
  • 13
  • 30
63
votes
11 answers

How to rewrite the protected/private route using TypeScript and React-Router 4, 5 or 6?

I was trying to create a as describe in the react-router documents using TypeScript. Can anyone help me out? The privateRoute in react-router document: const PrivateRoute = ({ component: Component, ...rest }) => (
Charlie
  • 2,141
  • 3
  • 19
  • 35
63
votes
5 answers

Transition to another route on successful async redux action

I have a pretty simple set of react components: container that hooks into redux and handles the actions, store subscriptions, etc list which displays a list of my items new which is a form to add a new item to the list I have some react-router…
Clarkie
  • 7,490
  • 9
  • 39
  • 53
62
votes
8 answers

useNavigate() may be used only in the context of a component

See my code below. I'm trying to add this button that goes back to the previous page using react-router-dom but I get the below error, and also all the components on my website disappear. Error: useNavigate() may be used only in the context of a…
kid
  • 771
  • 1
  • 4
  • 12
62
votes
9 answers

React-router - How to pass data between pages in React?

I am working on a project where I have to pass data from one page to another. For example, I have data on the first page. let data = [ {id:1, name:'Ford', color:'Red'}, {id:2, name:'Hyundai', color:'Blue'} ] Here is the first component page…
Andrew
  • 727
  • 1
  • 9
  • 12
61
votes
7 answers

Cannot read property 'history' of undefined (useHistory hook of React Router 5)

I am using the new useHistory hook of React Router, which came out a few weeks ago. My React-router version is 5.1.2. My React is at version 16.10.1. You can find my code at the bottom. Yet when I import the new useHistory from react-router, I get…
60
votes
5 answers

How to build a 404 page with react-router-dom v6

I just upgraded to v6beta for react-router-dom because I wanted to organize my routes, but the 404 page is now broken: export function AllRoutes(props) { return(
RamiroPastor
  • 1,085
  • 1
  • 7
  • 18
60
votes
17 answers

Protected route with react router v6

What is correct way to write a ProtectedRoute with new version 6 of react-router? I wrote this one, but it's not a route const PrivateRoute = ({ component: Component, ...props }) => { if (!Component) return null; return…
Victor
  • 745
  • 2
  • 7
  • 16