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
287
votes
35 answers

How to get rid of underline for Link component of React Router?

I have the following: How do I get rid of the blue underline? The code is below: Team 1 The MenuItem component is from…
Jo Ko
  • 7,225
  • 15
  • 62
  • 120
274
votes
9 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
  • 5,779
  • 11
  • 30
  • 64
267
votes
48 answers

React.createElement: type is invalid -- expected a string

Trying to get react-router (v4.0.0) and react-hot-loader (3.0.0-beta.6) to play nicely, but getting the following error in the browser console: Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a…
JoeTidee
  • 24,754
  • 25
  • 104
  • 149
250
votes
19 answers

How do you programmatically update query params in react-router?

I can't seem to find how to update query params with react-router without using . hashHistory.push(url) doesn't seem to register query params, and it doesn't seem like you can pass a query object or anything as a second argument. How do you…
claireablani
  • 7,804
  • 5
  • 16
  • 19
240
votes
14 answers

React: 'Redirect' is not exported from 'react-router-dom'

I am getting the following error when running npm run start in the terminal. Attempted import error: 'Redirect' is not exported from 'react-router-dom'. I have reinstalled node_modules, react-router-dom, react-router. Also restarted the terminal…
Zero Cool
  • 2,541
  • 2
  • 8
  • 13
231
votes
19 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
  • 2,333
  • 2
  • 11
  • 5
215
votes
12 answers

Multiple path names for a same component in React Router

I am using the same component for three different routes: Is there anyway to combine it, to…
Chetan Garg
  • 2,167
  • 2
  • 9
  • 3
209
votes
29 answers

react-router go back a page how do you configure history?

Can anyone please tell me how I can go back to the previous page rather than a specific route? When using this code: var BackButton = React.createClass({ mixins: [Router.Navigation], render: function() { return (
207
votes
16 answers

React-router and nginx

I am transitioning my react app from webpack-dev-server to nginx. When I go to the root url "localhost:8080/login" I simply get a 404 and in my nginx log I see that it is trying to get: my-nginx-container | 2017/05/12 21:07:01 [error] 6#6: *11…
martin
  • 3,289
  • 5
  • 22
  • 27
203
votes
10 answers

React-Router: No Not Found Route?

Consider the following: var AppRoutes = [ ,
4m1r
  • 12,234
  • 9
  • 46
  • 58
195
votes
11 answers

How to tell webpack dev server to serve index.html for any route

React router allows react apps to handle /arbitrary/route. In order this to work, I need my server to send the React app on any matched route. But webpack dev server doesn't handle arbitrary end points. There is a solution here using additional…
eguneys
  • 6,028
  • 7
  • 31
  • 63
194
votes
11 answers

Detecting user leaving page with react-router

I want my ReactJS app to notify a user when navigating away from a specific page. Specifically a popup message that reminds him/her to do an action: "Changes are saved, but not published yet. Do that now?" Should i trigger this on react-router…
Barry Staes
  • 3,890
  • 4
  • 24
  • 30
193
votes
17 answers

How to use Redirect in version 5 of react-router-dom of Reactjs

I am using the last version react-router module, named react-router-dom, that has become the default when developing web applications with React. I want to know how to make a redirection after a POST request. I have been making this code, but after…
maoooricio
  • 2,249
  • 3
  • 15
  • 19
187
votes
10 answers

Detect Route Change with react-router

I have to implement some business logic depending on browsing history. What I want to do is something like this: reactRouter.onUrlChange(url => { this.history.push(url); }); Is there any way to receive a callback from react-router when the URL…
185
votes
9 answers

How to manually invoke Link in React-router?

I have a component that receives through props a object from react-router. Whenever the user clicks on a 'next' button inside this component I want to invoke object manually. Right now, I'm using refs to access the backing instance…
Alan Souza
  • 7,475
  • 10
  • 46
  • 68