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

Testing react router v4 with jest enzyme

I'm trying to do a simple test with react router v4 and jest enzyme. describe('', () => { it('renders a static text', () => { const wrapper = shallow(
johnwick0831
  • 918
  • 1
  • 12
  • 24
6
votes
1 answer

How to show loading animation between React Router components

This is a basic situation: I have a Nav with links and several routes for those links in a main content area. const App = () => (
6
votes
1 answer

window.onpopstate - do I need to remove this event handler?

I'm using window.onpopstate event handler to listen for back and forward browser button events. My code looks something like this: componentDidMount() { window.onpopstate = this.onBackOrForwardButtonEvent; } onBackOrForwardButtonEvent = (e) =>…
user2456977
  • 3,830
  • 14
  • 48
  • 87
6
votes
1 answer

Why wont my Auth0 find my callback route in react?

Auth0 redirects to http://localhost:3000/callback#/acccess-token=dxy I'm getting a blank screen in my react app. Heres my main app.js render((
), $('#app')[0]); My main contains my…
Tyler Jones
  • 135
  • 10
6
votes
4 answers

Redirect in React with Typescript

I am working on React with Typescript. "Redirect" doesn't seem to work for me. I don't know what the problem is. import * as React from "react" import { TextField } from 'office-ui-fabric-react/lib/TextField'; import { PrimaryButton } from…
Yog
  • 61
  • 1
  • 2
  • 6
6
votes
0 answers

React Router causes Redux container components re-render unneccessary

Here is my major code, App component is connected to Redux's store: class App extends Component { render() { const { requestQuantity } = this.props; return (
Jason Xu
  • 845
  • 8
  • 22
6
votes
2 answers

setParams not working in react-native

I am using react-native with redux. I am trying to update current screen's params so that they can be accessed in a component used in top-bar but parameter is not getting set. My code is following: Screen Route: AlertNameForm: { screen:…
Adnan Ali
  • 2,890
  • 5
  • 29
  • 50
6
votes
2 answers

React - Uncaught TypeError: Cannot read property 'func' of undefined

I'm receiving the error: Uncaught TypeError: Cannot read property 'func' of undefined Yet I have no idea why, I've Googled the error and gone to everyone post with the same error yet no luck. Can anyone help me out? I'm using…
user8826104
6
votes
2 answers

How to test React Router params with Redux and enzyme

This has been resolved I have a component that is wrapped with connect() and withRouter. To render itself, it uses routing information in this.props.match.params. In my Enzyme test, the match prop that I pass to the WrappedComponent does not seem to…
Nosajool
  • 456
  • 4
  • 10
6
votes
2 answers

React Material-UI with Router

I'm trying to build simple React app using Material-UI library. I've used the example app for create-react-app and try to add Router to it so I can navigate between components but it seems that the components added by Material-UI library do not play…
Ido Ran
  • 10,584
  • 17
  • 80
  • 143
6
votes
2 answers

Duplicate paths in react-route

In heder there is a component with live search in which there are links for the found film or TV series depending on what came from the server. Use react-route v4 urlRusLat - just a function that returns translate name a movie
Drop
  • 1,394
  • 3
  • 15
  • 25
6
votes
3 answers

How to use react router?

I am working with reactjs and react router. I have create my router as: ReactDOM.render( , …
John
  • 103
  • 1
  • 2
  • 12
6
votes
1 answer

page is not rendering when I click back button with react-router

I have an application that has this top level component: import React from 'react'; import ReactDOM from 'react-dom'; import { BrowserRouter as Router } from 'react-router-dom'; import { Provider } from 'react-redux'; import { createStore } from…
Brandon
  • 1,735
  • 2
  • 22
  • 37
6
votes
3 answers

React Router Error related to Link To

I am trying to route the components from one to another, i am getting errors related to the react router link and the error for the react router is - Failed to compile i have tried using react router and added its package to my project library. here…
Husain Khanbahadur
  • 479
  • 1
  • 8
  • 22
6
votes
1 answer

React Router params returning empty object

In my App.js file where I render all my routes I have this code: import LiveMatch from "../containers/LiveMatch"; const routes = [ //removed other routes for simplicity of post { path: "/livematch/:matchid", <--- here is the param I want …
Phil
  • 3,342
  • 5
  • 28
  • 50