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
60
votes
11 answers

React Router - Typescript errors on withRouter after updating version

I just tried to upgrade my React app to react-router - 4.0.19 to 4.0.20 react- 16.0.30 to 16.0.34 typescript- version "2.7.0-insiders.20180108" In my app, wherever I am using 'withRouter', I now get cryptic Typescript errors. I even replaced all…
29er
  • 8,595
  • 12
  • 48
  • 65
60
votes
5 answers

React Router BrowserRouter leads to "404 Not Found - nginx " error when going to subpage directly without through a home-page click

I am using React Router for routing for a multi-page website. When trying to go to a sub page directly https://test0809.herokuapp.com/signin you'd get a "404 Not Found -nginx" error (To be able to see this problem you might need to go to this link…
Di Ye
  • 739
  • 1
  • 8
  • 14
60
votes
4 answers

How to fetch data when a React component prop changes?

My TranslationDetail component is passed an id upon opening, and based on this an external api call is triggered in the class constructor, receiving data to the state, and this data being displayed on TranslationDetail. //Routing:
balu000
  • 659
  • 1
  • 5
  • 9
60
votes
5 answers

How to combine ReactJs Router Link and material-ui components (like a button)?

I need to find a solution to be able to combine together the functionality of react router with the material ui components. For instance, I've this scenario: a router and a button. What I tried to do it is to mix them together, and restyle…
axel
  • 3,778
  • 4
  • 45
  • 72
59
votes
8 answers

Error: Cannot find file: 'index.js' does not match the corresponding name on disk: './node_modules/React/react'

Here is an image of the error and the console error... My code seems to be correct and the paths of the imports are good too, I don't understand why I'm getting this error. Notifications.js import React from 'React' const Notifications = () => { …
josephT
  • 822
  • 1
  • 7
  • 16
58
votes
3 answers

react-router Redirect vs history.push

I was reading react-router-redux examples and I confused, what is the difference beetween: import { Redirect } from 'react-router-dom' ... and import { push } from 'react-router-redux' ... push('/login')
58
votes
6 answers

Multiple Nested Routes in react-router-dom v4

I need multiple nested routes in react-router-dom I am using v4 of react-router-dom I've got my import { BrowserRouter as Router, Route } from 'react-router-dom'; and I need the components to render like so --- Login --- Home --- Page 1 ---…
Aditya Talpade
  • 763
  • 1
  • 9
  • 13
58
votes
2 answers

On React Router, how to stay logged in state even page refresh?

I'm making a website with React, React Router, and Redux. Lots of routes (pages) require users to be logged in. I can redirect to the login page if the user is not logged in like this: function requireAuth(nextState, replace) { let loggedIn =…
modernator
  • 4,341
  • 12
  • 47
  • 76
57
votes
7 answers

Using anchor tags in react-router 4

I want the page to scroll down to my anchor tags when a user navigates to it through an anchor link. I'm using react-router 4 and I've defined things as follows: navbar: export default (props) => { const { updateModal, updateRoute, } =…
S. Schenk
  • 1,960
  • 4
  • 25
  • 46
57
votes
7 answers

How to Access History Object Outside of a React Component

First of all, I am pretty familiar with the withRouter HoC, however, in this case, it doesn't help because I do not want to access the history object in a component. I am trying to achieve a mechanism that will redirect the user to the login page if…
Dragos Rizescu
  • 3,380
  • 5
  • 31
  • 42
57
votes
5 answers

React-Router is refreshing page when I follow a route using tag

I'm building a React app that has links pointing to predefined routes. Click Here The routes resolve fine, but it's refreshing the page, thereby slowing down app performance. How do I avoid re-rendering the entire…
Rick
  • 8,366
  • 8
  • 47
  • 76
56
votes
6 answers

is there a way to set a default route with React-Router v6

I just can't find a way to set a default route with react-router v6 Is it because it's not good programming anymore? Can somebody tell me why? Thanks in advance Rafael
Rafael
  • 2,413
  • 4
  • 32
  • 54
56
votes
4 answers

React - What is the best way to handle login and authentication?

New to react and working on an application with authentication/logging in. It currently works but feels hacked together. Right now I have my isAuthenticated state located in my routes.js like so: class Routes extends Component { …
Vincent Nguyen
  • 1,555
  • 4
  • 18
  • 33
56
votes
7 answers

How can I style active Link in react-router v4?

In react-router v3 we had activeStyle and activeClassName to style active Link we could do something like this
{tags.map(t =>
56
votes
2 answers

Firing Redux actions in response to route transitions in React Router

I am using react-router and redux in my latest app and I'm facing a couple of issues relating to state changes required based on the current url params and queries. Basically I have a component that needs to update it's state every time the url…
deowk
  • 4,280
  • 1
  • 25
  • 34