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
100
votes
10 answers

React Router V6 - Error: useRoutes() may be used only in the context of a component

I have installed react-router-domV6-beta. By following the example from a website I am able to use the new option useRoutes I have setup page routes and returning them in the App.js file. After saving I am getting the following error: Error:…
Galanthus
  • 1,958
  • 3
  • 14
  • 35
100
votes
16 answers

Intercept/handle browser's back button in React-router?

I'm using Material-ui's Tabs, which are controlled and I'm using them for (React-router) Links like this: }/>
high incompetance
  • 2,500
  • 4
  • 18
  • 26
98
votes
1 answer

React-Router - Link vs Redirect vs History

There seems to be some confusion with what to use over the other: history.push('/some/path') I have been using React/Router for a little while now, and different posts/answers say different things…
Phil
  • 3,342
  • 5
  • 28
  • 50
96
votes
29 answers

How do I add an active class to a Link from React Router?

I've created a bootstrap-style sidebar using Link. Here is a snippet of my code:
  • MAIN NAVIGATION
coderberry
  • 3,040
  • 3
  • 26
  • 28
95
votes
6 answers

How to sync Redux state and url query params

I have a web page with a search panel. Search panel has several input fields: id, size, ... What I want is when user set search values (for example: id=123 and size=45) and press a search button: searchState in Redux reducer should be updated with…
95
votes
10 answers

React-Bootstrap link item in a navitem

I'm having some styling issues using react-router and react-bootstrap. below is a snippet of the code import { Route, RouteHandler, Link } from 'react-router'; import AuthService from '../services/AuthService' import { Button, Nav, Navbar,…
chad schmidt
  • 1,022
  • 1
  • 10
  • 8
93
votes
8 answers

Jest, Enzyme: Invariant Violation: You should not use or withRouter() outside a

I have a which outputs one component and list of contacts presentated by . The problem is that in the test for when I try to mount it, test outputs an error Invariant Violation:…
Maria Piaredryj
  • 1,584
  • 3
  • 16
  • 35
91
votes
12 answers

Error: useHref() may be used only in the context of a component. It works when I directly put the url as localhost:3000/experiences

I have a navbar that is rendered in every route while the route changes on click. ./components/navbar.jsx import React, { Component } from 'react'; import '../App.css'; import { Link } from 'react-router-dom'; class Navbar extends Component { …
tendinitis
  • 936
  • 1
  • 5
  • 9
90
votes
12 answers

Is there a way to modify the page title with React-Router v4+?

I'm looking for a way to modify the page title when React-Router v4+ changes locations. I used to listen for a location change action in Redux and check that route against a metaData object. When using React-Router v4+, there's no fixed routes list.…
88
votes
25 answers

Invariant failed: You should not use outside a

I use react-router-dom for routing in my React application. Part of my app extracted in another package. List of dependencies looks like this: ./app/dashboard/package.json { "dependencies": { "@app/components": "^1.0.0", "react":…
Pavel Perevezencev
  • 2,596
  • 3
  • 28
  • 49
86
votes
17 answers

react-router v6: get path pattern for current route

Is it possible to get the path pattern for the currently matched route? Example: } /> // Page.jsx function Page() { ... // usePathPattern doesn't actually exist const…
caseyjhol
  • 3,090
  • 2
  • 19
  • 23
86
votes
8 answers

Why do I get the error "expressions must have one parent element", how do I fix this?

I'm relatively new to React and I'm wondering what's the standard here. Imagine I have a react-router like this one:
Wordpressor
  • 7,173
  • 23
  • 69
  • 108
84
votes
5 answers

react router difference between component and render

I really don't get the difference between render and component prop in Route in react router, in docs it says that render doesn't create new element but component does, I tried to go back with history but I found componentWillMount is called when I…
scully
  • 961
  • 2
  • 8
  • 13
84
votes
9 answers

How to get current route in react-router 2.0.0-rc5

I have a router like below: Here's what I want to achieve : Redirect…
wxtry
  • 2,058
  • 2
  • 12
  • 13
82
votes
8 answers

react-router-dom v6 useNavigate passing value to another component

The version of react-router-dom is v6 and I'm having trouble with passing values to another component using Navigate. I want to pass selected rows to another page called Report. But, I'm not sure I'm using the right syntax for navigate method and I…
Jay Kim
  • 825
  • 1
  • 5
  • 7