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
3 answers

React Router Scroll to Top on V6

When I change pages, the application is being kept at the same point it was on the previous page. I want to show the component from the top when I change pages. To achieve that, I am trying to implement React Router ScrollToTop. I found the…
5
votes
4 answers

react-router-dom v6 NavLink and MUI ListItem not working with className

I have a problem using MUI with react-router-dom v6. import ListItem from '@mui/material/ListItem'; import { NavLink } from 'react-router-dom';
kie
  • 194
  • 1
  • 9
5
votes
2 answers

Event Listener on Navigation with react router v6

I would like to add an event listener to a navigate event in react router v6. I have not found anything to it. Is there such a feature? Edit: When the event is handled, I want to prevent the navigation and route the user to another destination,…
CrystalCase
  • 137
  • 2
  • 11
5
votes
0 answers

Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self'" when page refreshed

I have a Node/React app and get this error when I refresh the page in my production build, for all routes apart from "/", and a blank page is shown. Refused to execute inline script because it violates the following Content Security Policy…
protoplan
  • 143
  • 7
5
votes
4 answers

Router and redirect in React.js

I am a beginner in react. I'm trying to set up router and rendering to change pages but it gives me errors that I can't understand. I have installed to terminal npm install react-router-dom in my index.js I have import BrowserRouter and embedded my…
Cimo
  • 165
  • 3
  • 7
5
votes
2 answers

React router v6 access route params and pass as props

In react router v6, how can I pass route params to a component without the need to use useParams() in the component? This is what I want to do:
ServerBloke
  • 802
  • 3
  • 15
  • 24
5
votes
1 answer

react router 6 route params

I am trying to set up some react router routes, using the newest v6 specifically. What I'd like to do is have a website with a rest-like hierarchy of pages, and be able to parse out the IDs easily. Specifically with two level of IDs on the path. I'm…
Miro
  • 5,307
  • 2
  • 39
  • 64
5
votes
2 answers

Sharing state across React router v6 routes using Context API

Looking through old tutorials it seems like React Router v5 had support for sharing state across different routes using the context API but I can't find anything on a similar usage for Router v6. React Router v5 Implementation of what I am trying to…
5
votes
4 answers

useNavigate not working react-router-dom v6

I'm using react-router-dom v6 and I'm accessing the from value from the location object and it gives the pathname but when executing navigate(from,{replace:true}) it does not work. const navigate = useNavigate(); const { state } =…
Haree Prasad
  • 103
  • 1
  • 2
  • 8
5
votes
4 answers

Losing useState Value on Refresh in React.js

I am sending an id from ProductListing Component and I am receiving that id using useParams in ProductDetail Component. In ProductDetail Component I am finding an object using find method and then I am setting it into singleProduct state. On refresh…
user16511963
5
votes
2 answers

React Router v6 child route not rendering

With React Router v6 I want to render the callback page. Although I'm not able to get the output from the element prop in my screen. I'm only getting the output from out and always a blank screen or a 404 for the callback page. What am I missing…
ronnyrr
  • 1,481
  • 3
  • 26
  • 45
5
votes
2 answers

Role based react-router

What is the best way to conditionally render routes in react-router based on user role. I have a case where not all roles have permission to view certain routes. And also I need to deal with subroutes. So if one of main routes is something like…
Ivan Rajkovača
  • 69
  • 1
  • 1
  • 3
5
votes
3 answers

React-router v6 Route composition. Is it possible to render a custom Route?

For learning purposes I am trying to create a custom Route, which is protected, using React-Router v6. As many people have tried before me in Forums or here, I get this error: Error: [ProtectedRoute] is not a component. Also if someone is…
5
votes
1 answer

React Router V6 change component based on query string

I tried several thing to match the query string and show component according to that but no luck, my code: } > } /> …
Den Pat
  • 1,118
  • 10
  • 17
5
votes
4 answers

React Router v6, How to place into a button and event handler

I'm trying something like this: import Button from '@material-ui/core/Button'; import { Link } from "react-router-dom"; return (