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
3 answers

React Router Link doesn't work with LeafletJS

Versions: react-router-dom 4.1.1 react-router-redux 5.0.0-alpha.4 react-leaflet 1.1.3 leaflet 1.0.3 Steps to reproduce I create a leaflet map. In which I add some markers. These markers have popups. In each of these popup I want to have a…
Vincent Audebert
  • 1,846
  • 2
  • 15
  • 28
6
votes
1 answer

React router get current location on main router component

I'd like to know how to get the current location from the component I've used to define my routes. For example, I have a component called Routes, which contains all the routes, like this: class Routes extends React.Component { render() { …
celsomtrindade
  • 4,501
  • 18
  • 61
  • 116
6
votes
2 answers

React router v4 not working on build

Im having trouble with react-router-dom working in production. While my app header and footer are rendered just fine, the router does not work and I can see the following comments where my routes should appear upon inspecting the html in Chrome…
6
votes
2 answers

Refreshing a react router page that has a dynamic url/params

I have several components displayed with react router that have dynamic url paths. An example path I have is When entering this component, I have a componentWillMount…
henhen
  • 1,038
  • 3
  • 18
  • 36
6
votes
4 answers

React Router v4.0 - Redirect to a page outside of Router scope

I'd like to be able to navigate to a home page outside of "subpage" defined in a < Router >. Here's my component. import * as React from 'react'; import { BrowserRouter as Router, Route, Link } from 'react-router-dom'; import { Search } from…
ononononon
  • 1,033
  • 4
  • 14
  • 25
6
votes
1 answer

"Fetch as Google" doesn't load React routes on S3 with CloudFront

I have a react-router site on Amazon S3, with 404 redirects set to hit index.html, where they find the code they need and the site does what it should. In Google Webmaster Tools, "Fetch as Google" for one of the routes gave error status: Not…
6
votes
2 answers

Redirecting from a search form to a results page in reactjs

I am currently developing my first reactjs app and am having difficulties navigating from a Search component to a Results component using react-router-dom. The search component accepts entries from the user, performs a get request with axios and…
Awemo
  • 875
  • 1
  • 12
  • 25
6
votes
1 answer

How do I pass props in react router v4?

I need to pass some data in Link component of react router v4, but I cannot find a way to do it. Basically, I have some links that are dynamically generated. I have linked these links with Link component in the following way. Upon clicking one of…
Zip
  • 5,372
  • 9
  • 28
  • 39
6
votes
1 answer

react-router not returning content for nested routes on page load

My react app works as expected for first level routes such as /, /foo, and /bar, both when using the apps navigation menu or when typing the url directly into the browsers address bar. However deeper urls such as /foo/bar only work when using the…
Jeemusu
  • 10,415
  • 3
  • 42
  • 64
6
votes
1 answer

Routing, Universal apps (Nodejs, React), Error (0 , _reactRouter.match) is not a function

I can't fix this error... I start server, everything is ok, since I refresh localhost:3000 Then it show me an error: TypeError: (0 , _reactRouter.match) is not a function I have installed "react-router": "^4.0.0" import Express from…
6
votes
1 answer

query string react-router path

I am using react-router 3.0.2 and trying to configure router path with query string. This is how I have configured my router:
abhi
  • 349
  • 2
  • 8
  • 24
6
votes
1 answer

REACT Uncaught TypeError .then is not a function

i'm doing function in my react component like this : const Logged = (props) => { const doLogout = () => { props.logout().then(() => browserHistory.push('/')); } return(
ukiyakimura
  • 151
  • 1
  • 5
  • 15
6
votes
1 answer

React Router v4 for Form

How can I use with a
? I am trying to avoid programatic routing as I see many warnings against it. Form has two fields:
Noitidart
  • 35,443
  • 37
  • 154
  • 323
6
votes
2 answers

React.createElement: type is invalid -- expected a string, but got: object

I've just upgraded to Webpack 2.2 today and have been reading through their guides, which are still a work in progress it seems. I am having difficulties setting up my application to use webpack-dev-server with hot module reloading. The guide I was…
6
votes
1 answer

React-router You cannot push the same route... force History.push / reload component

I'm trying to get my search bar working correctly. When making a search, router sends me to /list/(whatever I searched for) The searchbar is changing my route programmatically like so: var pathname = '/list/'; var query = { …
Chris
  • 185
  • 1
  • 4
  • 13