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

How can I create a subrouter with React Router v6?

Here is my current React Router implementation: const router = createBrowserRouter([ { path: "/", element: ( ) }, { path: "/about", …
5
votes
2 answers

How to test routing logic with React Router v6 and testing-library?

I migrated from React Router v5 to v6 following this tutorial. I want to test it with react-testing-library, but my old unit tests (using the pattern in this doc) stopped working. My app with React Router v6 is like this const router =…
lzl124631x
  • 4,485
  • 2
  • 30
  • 49
5
votes
1 answer

Type '{ path: string; element: Element; }' is not assignable to type 'IntrinsicAttributes & BrowserRouterProps'

Trying to set up routing with React-Router in my React-Typescript app but I am receiving error on my path keyword: Type '{ path: string; element: Element; }' is not assignable to type 'IntrinsicAttributes & BrowserRouterProps'. Haven't been able…
melly18
  • 87
  • 3
  • 11
5
votes
1 answer

Uncaught Error: useSubmitImpl must be used within a Data Router

I am using below versions, "formik": "^2.2.9" "react": "^18.2.0" "react-dom": "^18.2.0" "react-router-dom": "^6.4.0" "react-scripts": "5.0.1" "typescript": "^4.7.4" "web-vitals": "^2.1.4" "@types/react-router-dom": "^5.3.3" And below is my…
Ari4
  • 860
  • 7
  • 12
5
votes
1 answer

React 6.4.0 common header for all the router

I am starting react project with react-router-dom version 6.4.0, but not able to create common header for all the routes. App.js - This is the file where I am adding RouterProvider import logo from './logo.svg'; import './App.css'; import { Link,…
Vivek Kurmi
  • 138
  • 1
  • 11
5
votes
3 answers

React Router: What is the purpose of using alone without context?

For what I understand now, if we pass an Outlet with context, the props after the context could be passed into child, and outlet act as a template that pass those props into any child the router may render. My question is, what if we just set…
Taiwan No.1
  • 91
  • 1
  • 1
  • 7
5
votes
1 answer

React router v6 get all routes

Let's say I have a url path 'clients/devices/2', the routes are defined like this: }> } /> }> …
Pavel Horyna
  • 360
  • 1
  • 5
  • 17
5
votes
1 answer

Error: [undefined] is not a component. All component children of must be a or

I'm trying to upgrade the react-router-dom from v5 to v6 but I am getting this error message. Error: [undefined] is not a component. All component children of must be a or Anyone please help me with this…
r121
  • 2,478
  • 8
  • 25
  • 44
5
votes
1 answer

How to redirect in routes after async request?

I have this code in which I use react-router-dom v6 for routing class App extends Component { constructor(props) { super(props); this.state = { accounts: [], }; } componentDidMount() { getAccounts().then(r =>…
levensta
  • 147
  • 1
  • 9
5
votes
1 answer

How to change title of the page of React-Router v6+ in history menu

There are a few questions like this on SO, but they are old and outdated and solutions don't work anymore How can I change the title of the page so I don't have the same name in the History menu? I can't find anything in the documentation I've…
jcubic
  • 61,973
  • 54
  • 229
  • 402
5
votes
3 answers

Getting 404 | Page Not found Nextjs

I created my page routing with react-router v5 and everything works well. If I click on the link, it takes me to the page, but when I reload the page I get a "404 | Page Not Found" error on the page. import React, { useEffect, useState } from…
Aliyu Kamilu
  • 83
  • 1
  • 1
  • 5
5
votes
4 answers

Scroll to top on every transition react-router-dom v6

I'm trying to scroll on top of each page change with react-router dom v6. The code is scrolling back on top only on my home page and not on my character details page. I've tried many solutions but can't get them to work. I'm using …
Ziyaad
  • 63
  • 1
  • 5
5
votes
0 answers

Handling errors in React (Error Boundary, React-Query, Axios Interceptor)

I have a Create React App (that uses react-router v6) that is setup the following way to communicate with the API. At the top level - I wrapped my app in an ErrorBoundary (Sentry.ErrorBoundary). When fetching the API, the front-end calls a useQuery…
feyndev
  • 122
  • 5
5
votes
3 answers

Navigate using react-router-dom v6 after performing user action

I started learning react about 15 days back. The following code adds the post correctly but does not redirect to "/". I am using react-router-dom v6. render(){ return
Arijit Datta
  • 101
  • 1
  • 2
  • 10
5
votes
1 answer

How to run a function when user clicks the back button, in React.js?

I looked around and tried to find a solution with React router. With V5 you can use . I tried also to find a vanilla JavaScript solution, but nothing worked for me. I use React router v6 and histroy is replaced with const navigate =…
Fotios Tsakiris
  • 1,310
  • 1
  • 18
  • 24