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
73
votes
10 answers

How to parse query string in react-router v4

In react-router v3 I could access it with props.location.query.foo (if the current location was ?foo=bar) In react-router-dom@4.0.0 props.location only has props.location.search with is a string like ?foo=bar&other=thing. Perhaps I need to manually…
Peter Bengtsson
  • 7,235
  • 8
  • 44
  • 53
73
votes
3 answers

onEnter not called in React-Router

Ok, I'm fed up trying. The onEnter method doesn't work. Any idea why is that? // Authentication "before" filter function requireAuth(nextState, replace){ console.log("called"); // => Is not triggered at all if (!isLoggedIn()) { replace({ …
html_programmer
  • 18,126
  • 18
  • 85
  • 158
73
votes
4 answers

React router nav bar example

I am a beginner in React JS and would like to develop a react router based navigation for my Dashboard. The mockup is as follows: My app.js code which I created to try routing is as follows: import React from 'react' import { render } from…
cucucool
  • 3,777
  • 8
  • 48
  • 63
72
votes
7 answers

ReactJS Bootstrap Navbar and Routing not working together

I am trying to create a simple Webapp using ReactJS, and I wanted to use the Navbar provided by React-Bootstrap. I created a Navigation.js file containing a class Navigation to separate the Navbar and the Routing from the App.js file. However, both…
NotAName
  • 865
  • 1
  • 6
  • 8
72
votes
4 answers

How to make a Material UI react Button act as a react-router-dom Link?

How can I make a Material UI react Button component act as a Link component from react-router-dom without losing it's original style? Like changing the route on click. import Button from '@material-ui/core/Button';
72
votes
10 answers

How do I clear location.state in react-router on page reload?

I am currently passing my state on route change like below: View Details My logic is that if…
Pratish Shrestha
  • 1,712
  • 4
  • 17
  • 26
72
votes
6 answers

How to make a shared state between two react components?

I have 2 react components that need to share a state, react-router shows component A, which takes some inputs and adds it to its state, after the state has been successfully updated, I want to redirect to component B, where the user adds some more…
Jack
  • 735
  • 1
  • 5
  • 7
71
votes
7 answers

Simple Conditional Routing in Reactjs

How to implement conditional routing i.e. if and only if some conditions satisfies, then routing should occur. For example, if and only if the user enters the correct credentials, login should be successful and the user should be able to see the…
69
votes
8 answers

How to mock useHistory hook in jest?

I am using UseHistory hook in react router v5.1.2 with typescript? When running unit test, I have got issue. TypeError: Cannot read property 'history' of undefined. import { mount } from 'enzyme'; import React from 'react'; import {Action} from…
Ivan Martinyuk
  • 1,230
  • 1
  • 9
  • 13
69
votes
9 answers

How to use React Router with Electron?

Using this boilerplate as reference I created an Electron app. It uses webpack to bundle the scripts and express server to host it. Webpack config is practically same as this and server this. Electron's script loads: mainWindow.loadURL('file://' +…
Frozen Crayon
  • 5,172
  • 8
  • 36
  • 71
68
votes
5 answers

Warning: validateDOMNesting(...): cannot appear as a descendant of

I'm trying to use react-router with reactstrap with create-react-app. In the routing page, I needed to use state for the reactstrap, so I converted the router from a variable to a class, but I get this warning: Warning: validateDOMNesting(...):
68
votes
2 answers

Difference between using a HOC vs. Component Wrapping

I just checked out HOC's in React. They are pretty cool. However, doesn't simply wrapping a component achieve the same result? Higher Order Component This simple HOC passes state as properties to the ComposedComponent const HOC = ComposedComponent…
Tabbyofjudah
  • 1,973
  • 3
  • 17
  • 29
68
votes
11 answers

Warning: Failed propType: Invalid prop `component` supplied to `Route`

I'm trying new react-router 1.0.0 and I'm getting strange warnings I can't explain: Warning: Failed propType: Invalid prop `component` supplied to `Route`. Warning: Invalid undefined `component` supplied to `Route`. The app is simple: import…
Alex Kovshovik
  • 4,085
  • 4
  • 35
  • 36
67
votes
17 answers

React Router: Cannot read property 'pathname' of undefined

I've just started learning React and got stuck at this error. Uncaught TypeError: Cannot read property 'pathname' of undefined at new Router Here is my code: var React = require('react'); var ReactDOM = require('react-dom'); var { Route,…
Alon
  • 883
  • 1
  • 6
  • 18
67
votes
4 answers

Multiple params with React Router

I use React 15.0.2 and React Router 2.4.0. I want to pass multiple params to my route and I'm not sure how to do it in the best way:
G. Frx
  • 2,350
  • 3
  • 19
  • 31