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

15739 questions
1771
votes
36 answers

Programmatically navigate using React router

With react-router I can use the Link element to create links which are natively handled by react router. I see internally it calls this.context.transitionTo(...). I want to do a navigation. Not from a link, but from a dropdown selection (as an…
George Mauer
  • 103,465
  • 117
  • 349
  • 581
860
votes
50 answers

React-router urls don't work when refreshing or writing manually

I'm using React-router and it works fine while I'm clicking on link buttons, but when I refresh my webpage it does not load what I want. For instance, I am in localhost/joblist and everything is fine because I arrived here pressing a link. But If I…
DavidDev
  • 8,865
  • 3
  • 12
  • 15
620
votes
38 answers

Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function but got: object

I am getting this error: Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. This is my code: var React =…
Pankaj Thakur
  • 7,341
  • 3
  • 19
  • 27
555
votes
35 answers

React - How to get parameter value from query string?

How can I define a route in my routes.jsx file to capture the __firebase_request_key parameter value from a URL generated by Twitter's single sign on process after the redirect from their…
Franco
  • 8,739
  • 7
  • 25
  • 32
423
votes
23 answers

How to push to History in React Router v4?

In the current version of React Router (v3) I can accept a server response and use browserHistory.push to go to the appropriate response page. However, this isn't available in v4, and I'm not sure what the appropriate way to handle this is. In this…
Chris
  • 5,310
  • 5
  • 21
  • 28
354
votes
6 answers

React Router with optional path parameter

I want to declare a path with an optional path parameter, hence when I add it the page to do something extra (e.g. fill some data): http://localhost/app/path/to/page <= render the page http://localhost/app/path/to/page/pathParam <= render the…
tbo
  • 7,760
  • 6
  • 35
  • 46
346
votes
25 answers

react-router - pass props to handler component

I have the following structure for my React.js application using React Router: var Dashboard = require('./Dashboard'); var Comments = require('./Comments'); var Index = React.createClass({ render: function () { return (
Kosmetika
  • 19,249
  • 37
  • 94
  • 154
326
votes
12 answers

How to pass params with history.push/Link/Redirect in react-router v4?

How can we pass parameter with this.props.history.push('/page') in React-Router v4? .then(response => { var r = this; if (response.status >= 200 && response.status < 300) { r.props.history.push('/template'); …
IshanGarg
  • 3,309
  • 2
  • 9
  • 10
324
votes
11 answers

Nested routes with react router v4 / v5

I am currently struggling with nesting routes using react router v4. The closest example was the route config in the React-Router v4 Documentation. I want to split my app in 2 different parts. A frontend and an admin area. I was thinking about…
datoml
  • 4,594
  • 3
  • 17
  • 27
267
votes
8 answers

React Router v4 - How to get current route?

I'd like to display a title in that is somehow passed in from the current route. In React Router v4, how would be able to get the current route passed into it's title prop?
Raphael Rafatpanah
  • 15,663
  • 19
  • 73
  • 136
232
votes
6 answers

React : difference between and

Can someone explain the difference between and I don't know the meaning of 'exact'
batt
  • 2,382
  • 2
  • 8
  • 11
215
votes
6 answers

No restricted globals

I am using React and Redux to develop a webapp and when I started up my project I got this: Line 13: Unexpected use of 'location' no-restricted-globals Search for the keywords to learn more about each error. I search a lot about how to resolve…
Martin Nordström
  • 4,214
  • 6
  • 22
  • 48
183
votes
23 answers

react-router scroll to top on every transition

I have an issue when navigating into another page, its position will remain like the page before. So it won't scroll to top automatically. I've also tried to use window.scrollTo(0, 0) on onChange router. I've also used scrollBehavior to fix this…
adrian hartanto
  • 1,865
  • 2
  • 8
  • 9
173
votes
4 answers

react-router vs react-router-dom, when to use one or the other?

Both have Route, Link, etc. When to use one or the other? I'm really confused on where to use each one. Server side? Client side? https://reacttraining.com/react-router/ In some examples you need to pass the history, in others not. What to…
chulian
  • 2,507
  • 3
  • 19
  • 22
171
votes
12 answers

Pass props in Link react-router

I am using react with react-router. I am trying to pass property’s in a "Link" of react-router var React = require('react'); var Router = require('react-router'); var CreateIdeaView = require('./components/createIdeaView.jsx'); var Link =…
vishal atmakuri
  • 1,733
  • 2
  • 9
  • 5
1
2 3
99 100