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

React link to static html page?

i have a React + Redux app, i want to link to a privacy.html page which is a static html file in the root of the project along side with index.html which is the react app. The problem is that the link to /privacy.html is caught by the
Israhack
  • 459
  • 1
  • 6
  • 15
6
votes
1 answer

React Router serverside rendering errors: Warning: Failed propType: Required prop `history` was not specified in `RoutingContext`

I'm setting up a simple toy app to learn React/Hapi and everything is working well till I try and set up serverside routing. The server runs without error and renders "/" properly with hello world. However when I navigate to "/test" I get the…
Trattles
  • 647
  • 1
  • 6
  • 21
6
votes
1 answer

Is it possible to access the current route component with React Router

For example I have routes like: Lets assume that the current route is '#/about'. I need something like routerInstance.currentComponent to return AboutPage…
Dmitry Sokurenko
  • 6,042
  • 4
  • 32
  • 53
6
votes
2 answers

Endless loop rendering component on ReactJs

I'm facing an infinite loop issue and I can't see what is triggering it. It seems to happen while rendering the components. I have three components, organised like this : TimelineComponent |--PostComponent …
Yonirt
  • 165
  • 2
  • 2
  • 11
6
votes
1 answer

React-Router's Link-To updates the URL but doesn't refresh page

The header of my site has 10 category images (links). Each uses React-Router's Link to route to each category's respective categoryShow. The link works from categoryIndex, but it no longer works when being clicked form a cagetoryShow. It properly…
TCannadySF
  • 286
  • 7
  • 20
6
votes
2 answers

Firing callback on query change in react router

I'm looking for solution for paging in routing with react-router and redux. React-router don't fire callback in onEnter hook if only query changes, Router.run method is deprecated, so I'm a bit puzzled. Are there any other thing to do besides…
Victor Suzdalev
  • 2,202
  • 19
  • 28
6
votes
2 answers

How do I get a hold of the store/dispatch in React-Router onEnter?

I'm not using Redux-Router (maybe I have to?) but my router is wrapped by Provider and the store is being passed to that. I'd like to read state and dispatch in the onEnter handler.
kjs3
  • 5,758
  • 8
  • 34
  • 49
6
votes
3 answers

Required context `router` was not specified. Check the render method of `RoutingContext`

My app is ES6 React application with react-router. I want to redirect user to a different page after a small delay. Here is my React component: import React from 'react' import { Navigation } from 'react-router' export default class Component…
zaaath
  • 737
  • 9
  • 17
6
votes
3 answers

With getComponent how to pass props?

I can't figure out how to pass props to the component. This is important as I don't want to fetch data in the componentDidMount method as it will then be invisible to search engines. My code looks like this const router =
Ally
  • 4,894
  • 8
  • 37
  • 45
6
votes
5 answers

Reactjs router match callback parameters are always undefined

I use the match method for my server side rendering and the parameters in the callback are always undefined. Probably got something wrong but it's been a complete day and I can't get my head around it. Here's my server side rendering. // Create…
Raphael Parent
  • 474
  • 5
  • 9
6
votes
0 answers

Need to maintain scroll position when back button is pressed using history goBack function

I have a list of products and when particular product is clicked it opens a detail view page. I am using "this.history.goBack()" which is triggered by a button to go back to list of products. My problem is that every time i hit back button product…
rosnk
  • 1,068
  • 1
  • 14
  • 36
6
votes
1 answer

Optional React Router parameter

I'm trying to create a route that matches all of the following URLs: /product/foo /product/foo/bar Here's my current route: According to the documentation on…
Andrew Rasmussen
  • 14,912
  • 10
  • 45
  • 81
6
votes
1 answer

How to logically combine react-router and redux for client- and server-side rendering

I'd like my React based SPA to render on server side (who's not these days). Therefore I want to combine React with react-router, redux and some build layer like isomorphic starterkit. There is hapi universal redux which joins all together, but I am…
Jason Nerer
  • 551
  • 7
  • 19
6
votes
2 answers

React +(Router) without webpack or browserify

Is it possible to use react with ReactRouter, without using browserify or webpack. I am following the documentation from http://rackt.github.io/react-router they require react and react-router (require('react-router');). If I use browerifly my…
svenhornberg
  • 14,376
  • 9
  • 40
  • 56
5
votes
1 answer

How can you wrap a story with a react-router layout route?

I have a very simple and plain ComponentX that renders some styled HTML, no data fetching or even routing needed. It has a single, simple story. ComponentX is meant to be used in a dark-themed website, so it assumes that it will inherit color:…
ivanjonas
  • 599
  • 7
  • 19