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 to pass state to another component using useHistory?

I need to pass the state from the useState hook to another component, but the only connection between them is one button that has an onClick function to history. push(route). Table Page (from this page I must send state to TableMore page…
bajrax
  • 47
  • 3
5
votes
2 answers

How to clear browser back button history in react-router-dom

I am designing a Web Application using ReactJS. In here, I am using react-router-dom for navigation purposes. Here,it looks like import { BrowserRouter, Route, Routes } from 'react-router-dom'
rafekas
  • 191
  • 1
  • 2
  • 6
5
votes
4 answers

Error: useRoutes() may be used only in the context of a component even if the app is wrapped around BrowserRouter

I need to route between 2 paths, I installed npm install react-router (react-router: 6.2.1 and react-router-dom: 5.2.0). I get a error saying in the webpage Error: useRoutes() may be used only in the context of a component. which eventually…
Sai Krishnadas
  • 2,863
  • 9
  • 36
  • 69
5
votes
2 answers

isActive style in react-router v.6

Could you please help me to understand why active style is all the time active? I've got "react-router-dom": "^6.1.1". I tried different variety of way to apply this stylies the way it's written in react router documentation, but still i can't find…
Elizabeth
  • 1,271
  • 2
  • 5
  • 11
5
votes
2 answers

React - redirect to login page when not authenticated

This is my primary react file: // App.tsx const App: FC = () => { const isLoggedIn: boolean = localStorage.getItem('logged_user') !== null; return (
5
votes
2 answers

How to call a function from a Route in v6

Previously, in react-router-dom v5 (or below) you could use: {doSomething(); return }} /> to both fire a function and create a route without having to create any extra code inside of the ComponentName…
DORRITO
  • 621
  • 2
  • 8
  • 25
5
votes
4 answers

How to navigate react page on ternary operators?

I'm trying to navigate to admin page if LoggedIn and admin is true while sending props to /admin but this isn't working. Can you help please? export default function Auth() { function login(e) { e.preventDefault(); const data = {…
Firas SCMP
  • 461
  • 4
  • 18
5
votes
1 answer

How to setup index route on nested parent route in React Router v6

Details: I have top level routing like this: } /> } /> } />
moze
  • 322
  • 1
  • 5
  • 14
5
votes
1 answer

Page layout broken in React Router v6

I was refactoring my React app after updating React Router to v6 and I got rid of the error I was getting in my routes, except now the desired layout is broken. I need to include a permanent toolbar and a sidebar to be visible only in some pages. I…
Alex Braga
  • 495
  • 1
  • 6
  • 19
5
votes
2 answers

React-router v6: Cannot read properties of undefined (reading 'pathname')

I'm using react-router v6 and I'm trying to use a custom history to be able to navigate in redux actions (outside of a component). So I'm using Router from 'react-router instead of BrowserRouter from 'react-router-dom as mentionned in the doc. Here…
Simon
  • 209
  • 4
  • 12
5
votes
1 answer

React google map wont update styles after changing theme (component re-render)?

I have a custom theme in my website (light/dark) and I have a custom hook for that also. I put google map (react-google-maps) custom style in a useMemo and set theme as the dependancy but when I change theme it seems that map component doesn't…
EshgheCode
  • 288
  • 4
  • 19
5
votes
2 answers

Flickering when transitioning to a React.lazy loaded component

On this repo: https://github.com/tlg-265/react-app-vanilla $ git clone https://github.com/tlg-265/react-app-vanilla $ cd react-app-vanilla $ yarn $ yarn start I have a dummy app with just 3 pages: { Page1, Page2, Page3 }. My goal is: Split and lazy…
Viewsonic
  • 827
  • 2
  • 15
  • 34
5
votes
2 answers

How to pass additional props throughout a Route element that has parameters in a Typescript based React Web Application

I have a Functional Component in React in which was defined a Switch Component with some Routes. I want to pass additional props in one of these Routes (one that has parameters too), in order to use it inside the component that I will to mount when…
doDDy
  • 75
  • 7
5
votes
2 answers

How do I prevent the css in one component from affecting the css in another component in React?

I'm new to React, so I'm not exactly sure why this is happening. Using the React Router, I have three routes, /, /signup, and /login, and the importing of css in each component spills over into the css of the other components, ruining the styling.…
Darien Miller
  • 651
  • 2
  • 7
  • 16
5
votes
0 answers

React Router Dom v6: Prevent unmounting parent components

I try to prevent the unmount/rerender of parent components when navigating to another sub route. To achieve this I migrated to v6 since I expected that with the new relative routing this problem would be solved but it's not. Imagine the following…
mannitou
  • 123
  • 1
  • 9
1 2 3
99
100