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
50
votes
6 answers

React component render is called multiple times when pushing new URL

I am building a PhotoViewer that changes photos when the user presses left or right. I am using React, Redux, react-router, and react-router-redux. When the user presses left or right, I do two things, I update the url using this.context.replace()…
egidra
  • 8,537
  • 19
  • 62
  • 89
50
votes
11 answers

How to set activeClassName for wrapper element of Link or IndexLink in react-router?

I am new to the ReactJS world, and would like to know how can I pass active class name to the
  • element instead of (Link) element. Now I have this kind of code. The anchor class changes when clicked.
  • abekenza
    • 1,090
    • 2
    • 11
    • 19
  • 49
    votes
    13 answers

    withRouter' is not exported from 'react-router-dom

    After npm i --save react-router-dom and npm install --save with-router I tried to write import {withRouter} from 'react-router'; But I Get this error Attempted import error: 'withRouter' is not exported from 'react-router'. import React from…
    apanay22
    • 567
    • 1
    • 5
    • 10
    49
    votes
    3 answers

    required url param on React router v5 with typescript, can be undefined

    I am using react-router v5.1 with TypeScript and have this route configurations: and I…
    Or Bachar
    • 1,451
    • 3
    • 15
    • 20
    49
    votes
    2 answers

    Check history previous location before goBack() react router v4

    My goal is to enable a "Go Back" button, but only if the route/path the user would go back to is in a certain category. More precisely I have two kinds of Routes : / and /graph/. When the user navigates between graphs they…
    ted
    • 13,596
    • 9
    • 65
    • 107
    48
    votes
    6 answers

    How to open a page in new tab on click of a button in react? I want to send some data to that page also

    I'm working on a raise invoice page, in which user can raise a invoice on clicking of a button, I would call a api call and after getting the response I want to send some data to a page(RaisedInvoice.jsx) which should open in a new tab, how can i do…
    Manikanta B
    • 1,083
    • 2
    • 16
    • 30
    48
    votes
    4 answers

    Passing values through React-Router v4

    Question: How can I pass a prop or a single value, like an _id, through React-Router's Link component, and catch it at the endpoint? This is what I mean: Let's say we are on page /a. The Link will take the user to /b. As such . Now, I…
    jsdev17
    • 1,050
    • 2
    • 15
    • 25
    48
    votes
    5 answers

    React router 4 does not update view on link, but does on refresh

    I am using the following simple nav code
    ilyo
    • 35,851
    • 46
    • 106
    • 159
    48
    votes
    7 answers

    I try to make my Material-UI RaisedButton link to an external url without success?

    As the question in the title state. Playing with react, react-router and material-ui and in one place I want to make an action button in a Card component link to an external url, like without success. I'm currently thinking of adding an event…
    HenrikGr
    • 626
    • 1
    • 6
    • 12
    47
    votes
    7 answers

    react router v6 navigate outside of components

    In react-router v5 i created history object like this: import { createBrowserHistory } from "history"; export const history = createBrowserHistory(); And then passed it to the Router: import { Router, Switch, Route, Link } from…
    kofyohugna
    • 547
    • 1
    • 4
    • 7
    47
    votes
    4 answers

    react link vs a tag and arrow function

    I just started on react router. I have two questions. What is the difference between using and ? Both make the exact same get request to /page but I get an error when I use but it works when I use…
    forJ
    • 4,309
    • 6
    • 34
    • 60
    47
    votes
    2 answers

    React Router - how to constrain params in route matching?

    I don't really get how to constrain params with, for example a regex. How to differentiate these two routes? And…
    Sifnos
    • 1,161
    • 2
    • 13
    • 22
    46
    votes
    9 answers

    React router private routes / redirect not working

    I have slightly adjusted the React Router example for the private routes to play nice with Redux, but no components are rendered when Linking or Redirecting to other 'pages'. The example can be found…
    Rein
    • 1,347
    • 3
    • 17
    • 26
    46
    votes
    14 answers

    React-router TypeError: _this.props.history is undefined

    I am using react-router with react js and i following their documentation but facing this error while compiling it shows the error, TypeError: _this.props.history is undefined this is my index.js file import React from 'react'; import ReactDOM from…
    Manzurul Hoque Rumi
    • 2,911
    • 4
    • 20
    • 43
    45
    votes
    2 answers

    How to remove query param with react hooks?

    I know we can replace query params in component based classes doing something along the lines of: componentDidMount() { const { location, replace } = this.props; const queryParams = new URLSearchParams(location.search); …
    Seth McClaine
    • 9,142
    • 6
    • 38
    • 64