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
6
votes
1 answer

Symfony and React router, no route found

I am using Symfony3 and I am creating a bundle using React.js using itself react-router. The issue is when I use the routing in react, if I reload the page the symfony routing module send 'No Route Found" My routes are /admin for the index page and…
Cifren
  • 364
  • 1
  • 4
  • 15
6
votes
1 answer

redirect to a page programmatically in react-router 2

I am using react-router-2. I want to redirect to a page programmatically after successful login or after some action performed. My route file is like this(routes.js)
WitVault
  • 23,445
  • 19
  • 103
  • 133
6
votes
1 answer

How to make redirect inside component in React router?

How to make redirect inside component in React router? I'm using react-rouer import React from 'react' import _ from 'lodash'; import FavoriteItem from '../components/FavoriteItem' const FavoritesList = ({list}) => { let transitionToItem =…
Alexandr
  • 817
  • 1
  • 8
  • 16
6
votes
1 answer

Show custom dialog setRouteLeaveHook or history.listenBefore react-router/redux?

I cant seem to figure out how to show a custom dialogue instead of using the normal window.confirm that routeWillLeave and history.listenBefore uses. Basically i have built a notification system and check if a form is dirty const { dispatch,…
Deep
  • 2,988
  • 4
  • 34
  • 44
6
votes
1 answer

React Router redirect IndexRoute to proper path

I'm trying to configure React Router so that when accessing http://url/manage/roomId it goes directly to http://url/manage/roomId/sessions (loading RoomSessions component). These are tabs components' routes and I want to load the first tab's content…
Gab
  • 2,216
  • 4
  • 34
  • 61
6
votes
3 answers

React Router + AWS Backend, how to SEO

I am using React and React Router in my single page web application. Since I'm doing client side rendering, I'd like to serve all of my static files (HTML, CSS, JS) with a CDN. I'm using Amazon S3 to host the files and Amazon CloudFront as the…
Andrew Rasmussen
  • 14,912
  • 10
  • 45
  • 81
6
votes
4 answers

React router with browserHistory goes to server on every URL change

I am doing something like: {routes} When I do above whenever URL in address bar changes call is going to server but this is not what I want, I want first time page to load from server but after that…
Abhinav SInghvi
  • 391
  • 1
  • 4
  • 13
6
votes
2 answers

Passing additional parameters in React Router

How can I pass additional parametrs to the component that I am transitioning to. I have my routes.js as follows. I have declared two paths one for authorList and another for a particluar author's details. var routes = (
Aniket
  • 4,926
  • 12
  • 41
  • 54
6
votes
2 answers

Is there a clean way to conditionally load and render different components for the same React Router route?

The use case is that I want to map the root (/) to one of two different components based on whether the user is logged in or not, and I want these two components to reside in different bundles and lazily loaded, so simply putting the login check in…
Gabriel
  • 862
  • 6
  • 18
6
votes
2 answers

React router hot-reload not working

I have a small problem there are few similar questions on SA already but I can't figure out how to fix this. I am using react-router-redux but for this example I don't think there are any differences to using react-router I have a simple…
Kocur4d
  • 6,701
  • 8
  • 35
  • 53
6
votes
2 answers

Server Side React Initial Render causes duplicated async calls

I'm using react-router 2.0 on the server. Some of my top-level route components depend on async data to be fetched and uses Redux to manage state. To deal with this, I'm using a static fetchData() method to return a Promise.all of async actions as…
6
votes
1 answer

Testing href value from a link component while having dynamic props

I am looking for a solution in order to still be able to use Link from react-router instead of a when testing href attribute value. Indeed, I have some components which change of route according to the context. However, when I am testing the href…
Finstairn
  • 93
  • 1
  • 6
6
votes
1 answer

Using react-router w/ brunch/babel

I'm attempting use react-router in my brunch/babel setup. In my app.js I have: import React from "react" import ReactDOM from "react-dom" import { Router, Route, Link } from "react-router" This however gives me: Uncaught Error: Cannot find module…
Kyle Decot
  • 20,715
  • 39
  • 142
  • 263
6
votes
3 answers

ReactJS react-router RoutingContext

I'm building isomorphic application using ReactJS with react-router module for routing purposes on server side. From its guide about using react-router on server: (req, res) => { match({ routes, location: req.url }, (error, redirectLocation,…
oleh.meleshko
  • 4,675
  • 2
  • 27
  • 40
6
votes
2 answers

typescript definition for react-router v2.0 - error `has no exported member 'browserHistory'`

I am using react-router v.2.0.0.-rc3 and typescript. I installed the definition file via tsd. Since they changed the browserHistory from beeing a mixin to a module, I get the error Module '"react-router"' has no exported member 'browserHistory' on…
Felix Hagspiel
  • 2,634
  • 2
  • 30
  • 43