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

How to test components using new react router hooks?

Until now, in unit tests, react router match params were retrieved as props of component. So testing a component considering some specific match, with specific url parameters, was easy : we just had to precise router match's props as we want when…
Remi Deprez
  • 1,143
  • 2
  • 6
  • 5
114
votes
11 answers

React-router v4 - cannot GET *url*

I started to use react-router v4. I have a simple in my app.js with some navigation links (see code below). If I navigate to localhost/vocabulary, router redirects me to the right page. However, when I press reload (F5) afterwards…
exoslav
  • 2,094
  • 3
  • 21
  • 26
113
votes
1 answer

React-Router : What is the purpose of IndexRoute?

I don't understand what the purpose of using an IndexRoute and IndexLink. It seems that in any case the code below would have selected the Home component first unless the About path was activated.
Nick Pineda
  • 6,354
  • 11
  • 46
  • 66
113
votes
6 answers

How to stop /#/ in browser with react-router?

Any way to prevent /#/ from showing in the browser's address bar when using react-router? That's with ReactJS. i.e. Clicking on links to go to a new route shows localhost:3000/#/ or localhost:3000/#/about. Depending on the route.
Giant Elk
  • 5,375
  • 9
  • 43
  • 56
111
votes
5 answers

What TypeScript type should I use to reference the match object in my props?

In my React containers/component, which type could I use to reference the match part included by React Router DOM? interface Props { match: any // <= What could I use here instead of any? } export class ProductContainer extends…
Nicolas Blanco
  • 11,164
  • 7
  • 38
  • 49
111
votes
12 answers

Use anchors with react-router

How can I use react-router, and have a link navigate to a particular place on a particular page? (e.g. /home-page#section-three) Details: I am using react-router in my React app. I have a site-wide navbar that needs to link to a particular parts of…
Don P
  • 60,113
  • 114
  • 300
  • 432
109
votes
5 answers

How do I pass state through React_router?

Here is the file that's causing me trouble: var Routers = React.createClass({ getInitialState: function(){ return{ userName: "", relatives: [] } }, userLoggedIn: function(userName, relatives){ this.setState({ …
Jonas Bergner
  • 1,663
  • 4
  • 15
  • 22
109
votes
15 answers

Detect previous path in react router?

I am using react router. I want to detect the previous page (within the same app) from where I am coming from. I have the router in my context. But, I don't see any properties like "previous path" or history on the router object. How do I do it?
vijayst
  • 20,359
  • 18
  • 69
  • 113
108
votes
8 answers

The prop `history` is marked as required in `Router`, but its value is `undefined`. in Router

I am new to ReactJs. This is my code: var React = require('react'); var ReactDOM = require('react-dom'); var {Route, Router, IndexRoute, hashHistory} = require('react-router'); var Main = require('Main'); ReactDOM.render(
Mammad2c
  • 1,255
  • 2
  • 10
  • 13
106
votes
19 answers

Target Active Link when the route is active in Next.js

How to target the active Link in Next.js like they way we do it in React-Router-4? Meaning, give the active link a class when its route is active?
Ruby
  • 2,207
  • 12
  • 42
  • 71
106
votes
12 answers

How to get query parameters in react-router v4

I'm using react-router-dom 4.0.0-beta.6 in my project. I have a code like following: And I want to get query params in HomePage component. I've found location.search param, which looks like this:…
andrfas
  • 1,320
  • 2
  • 10
  • 16
106
votes
8 answers

How to restrict access to routes in react-router?

Does anyone know how to restrict access to particular routes in react-router? I want to check if the user is logged in before allowing access to a particular route. I thought it would be simple, but the docs aren't clear how to do it. Is this…
Tanner Semerad
  • 12,472
  • 12
  • 40
  • 49
105
votes
6 answers

How to render an array of objects in React?

could you please tell me how to render a list in react js. I do like this https://plnkr.co/edit/X9Ov5roJtTSk9YhqYUdp?p=preview class First extends React.Component { constructor (props){ super(props); } render() { const data…
user944513
  • 12,247
  • 49
  • 168
  • 318
103
votes
2 answers

react-router getting this.props.location in child components

As I understand will gives App routing-related props like location and params. If my App component has many nested child components, how do I get the child component to have access to these props without: passing…
xiaofan2406
  • 3,250
  • 5
  • 26
  • 34
101
votes
3 answers

Can I set a base route in react-router

Assuming my app's base url is example.com/app Is it possible to set a base route in react-router so instead of writing all routes as /app/a /app/b /app/c I can just specify them as a b c I tried the below example I found in the docs but it…
galki
  • 8,149
  • 7
  • 50
  • 62