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

React Router Redirect Conditional

I'm trying to make a button that only redirects the user to a new page after validation is completed correctly. Is there a way of doing something like this? How to I get a Route to be activated inside of a class method? import validator from…
jaimefps
  • 2,244
  • 8
  • 22
  • 45
6
votes
1 answer

React: Load component's CSS only when component is rendered

# MyComponent.js import React from 'react' import './MyComponentStyle.css' export default class MyComponent extends React.Component { .... } # App.js import React from 'react' import ReactDOM from 'react-dom' import { Route, Switch,…
Falloutcoder
  • 991
  • 6
  • 19
6
votes
1 answer

Change a single parameter in URL

I want to be able to create a link to the same route I'm on, except for changes to some parameters. In my website, I decided to have a URL structured like this: /list/:page(\d+)?/:filter?. Now, in my component I have access to match (using…
Misiur
  • 5,019
  • 8
  • 38
  • 54
6
votes
1 answer

React Router v4 Redirect with parameters

I have a route set up: In a component I am trying to redirect based on the entered name: I want to pass this.state.name to the Redirect, like /search/name. How?
Matt Saunders
  • 4,073
  • 7
  • 33
  • 47
6
votes
4 answers

Using React context to maintain user state

I'm trying to use React's context feature to maintain information about the user throughout the application (e.g. the user ID, which will be used in API calls by various pages). I'm aware that this is an undocumented and not recommended over Redux,…
Jess
  • 1,515
  • 3
  • 23
  • 32
6
votes
1 answer

Routes are not navigating when React v15.5 setup with react-redux v5 is

I am new with React and I have setup my React project with Facebook's create-react-app. Here are the core files: Index.js import React from 'react'; import ReactDOM, { render } from 'react-dom'; import { BrowserRouter as Router } from…
6
votes
1 answer

Why instead of ?

I have a submenu with Routes in my /About. This submenu is called AboutMenu and is present at all pages under /About like => /About/Company and /About/Info. An exercise example shows with activeStyle={match.isExact…
Hyrule
  • 635
  • 2
  • 10
  • 23
6
votes
1 answer

React Router changing params doesn't fire componentWillRecieveProps

The Problem: Changing the parameters of a component does not update the component it is rendering. The route change is shown in the URL bar, but directly rendering {this.props.match.params.id} shows the old :id and not the new one…
Connorelsea
  • 2,308
  • 6
  • 26
  • 47
6
votes
2 answers

Go back 2 pages - React-Router - ReactJS -

I have an component that it goBack to the previous page. I use that: this.props.history.goBack(); but I wanna that go back two levels. I have tried a lot of thing as this.props.history.goBack(-2); but I do not achieve that. I am using ReactRouter…
JuMoGar
  • 1,740
  • 2
  • 19
  • 46
6
votes
2 answers

How to redirect with flash message react-router 4

Here's what i'm trying to achieve: When a user tries to access a protected page on my ReactJS site, i want to redirect them to the home page with a flash message saying "Please log in" or something similar. How do i achieve this with react-router…
ss_millionaire
  • 429
  • 2
  • 7
  • 22
6
votes
1 answer

redirect in react-router undefined?

This is what my react-router code looks like render((
John
  • 32,403
  • 80
  • 251
  • 422
6
votes
2 answers

checking router.isActive on nested Routes in React

I'm trying to check routes with Router.isActive If the current route is /animals/edit (router.isActive('/animals/edit') is true. But if the current route is /animals/edit/23 (router.isActive('/animals/edit/') is false. How can I make a route…
lost9123193
  • 10,460
  • 26
  • 73
  • 113
6
votes
0 answers

React-Router returns 404 on child routes

I'm using react, react-router(v4), redux and webpack(-dev-server) to build an app. The Devserver and eveything works fine, including HMR and historyApiFallback. Means that when I enter http://localhost:8080/how I get index.html, bundle.js (my app),…
6
votes
2 answers

What is the difference between "push" method from react-router-redux and "browserHistory" from react-router?

I am trying to update history in react when the user navigates from one page/route to another. But confused about what method I should use to achieve this and why? import { browserHistory } from 'react-router' browserHistory.push('/bag') OR import…
Jyoti Duhan
  • 988
  • 1
  • 16
  • 26
6
votes
3 answers

How to maintain the state of React/Redux application across page refreshes?

I am new to react js. I have two routes like A & B. Now i am passing some values from A to B as props. If B page is refreshed, then all props values from A is gone and B page is not rendering. I am using react with redux. mapDispatchToProps &…
AndroidKid
  • 189
  • 3
  • 14