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
1 answer

How do I use react router with relay?

I am struggling with react-router and relay integration. As of now, I stick to this example It uses the useLazyLoadQuery hook, and although everything seems to work just fine, I also see another way of doing this: usePreloadedQuery. The docs say…
5
votes
3 answers

'Router' cannot be used as a JSX component

Error Message 'Router' cannot be used as a JSX component. Its return type 'void' is not a valid JSX element. TS2786 import App from './App'; 5 | > 6 | ReactDOM.render(, document.getElementById('root')); index.tsx…
user16356642
  • 103
  • 1
  • 1
  • 7
5
votes
2 answers

Webpack module federation lazy loading remoteEntry.js

Am I able to lazy load apps' entry files when I am using React with ReactRouter? When I enter page there are many requests for remoteEntry.js files for each app I have. I do not want to fetch it at the beginning because of performance. I want to…
5
votes
1 answer

SSR with data fetch without state management library

I am trying to make a SSR react app, but not able to pass props from express to the component. What mistake am i doing? server.js import AppPage2 from './src/project02/LandingPage'; ...... ...... server.get('/project2',async (req,res)=>{ const…
5
votes
1 answer

Client-side routing for S3 / Cloudfront with multiple path-based Single Page Apps

I have the following situation: An S3 bucket with multiple path-based applications, grouped by version number. Simplified example: /v1.0.0 index.html main.js /v1.1.0 index.html main.js Each application is a (React) SPA and requires…
5
votes
1 answer

Dynamic routing with next export mode

We're using Next.Js in next export mode (static HTML export), and we need advanced dynamic routing. Our routes will look like /[config1]/[config2]/[optionalConfig?]/page, where one segment is optional and the page names are fixed. For example…
TrueWill
  • 25,132
  • 10
  • 101
  • 150
5
votes
1 answer

Functional Component in React is not updating on props change

I am new to react and I have created one listContactComp which shows the list and it has edit button to that specific element. On the click on edit button I am calling editContactComp which is taking id as prop and fetching the specific contact…
Digvijay Rathore
  • 637
  • 1
  • 6
  • 21
5
votes
2 answers

React router - pass api data to the linked component to open with a new page

I am struggling to understand to design the routing here. For example - array.map((each)=>( )) by clicking on these cards, the skeleton of detailed page will open but how am i…
5
votes
1 answer

Using history.block with asynchronous functions/callback/async/await

I have a form inside a route, that if there are any validation errors, it should not allow the user to navigate to another route. If there are no validation errors, then allow navigation to another route. Below is my current code, which the onBlock…
mcclosa
  • 943
  • 7
  • 29
  • 59
5
votes
5 answers

How is it possible to access homepage from package.json in a react app?

I created a react app that will be served from /example-path. I defined it in the package.json like this: "homepage":"/example-path" It has worked so far, but now I would like to add routing with react-router-dom, and it incorrectly detects…
Iter Ator
  • 8,226
  • 20
  • 73
  • 164
5
votes
0 answers

Why use useHistory goBack function instead of window history back function in React App?

I implement page navigations with react-router. When moving from B to A after moving from A to B, needs page back behavior. Therefore, I wrote bellow. import { useHistory } from 'react-router'; const history = useHisotry() history.goBack(); It…
wato9902
  • 619
  • 7
  • 18
5
votes
3 answers

React lazy/Suspens + React Router dont change route until component is fetched

I am developing an application that uses the default React code spltting using the Lazy/Suspense approach and the React Router for component rendering. Currently, when I navigate to another path, if the network speed is slow, the path is updated and…
cmpeguerog
  • 537
  • 2
  • 7
  • 12
5
votes
1 answer

React table Gives empty table in build

This is a very strange issue but I was using react-table 7.0.0.rc16 and I recently upgraded to react-table 7.0.1 the problem is my data works in dev mode but as soon I create a react build it wont render anything I would like to know why and I am…
vinayak shahdeo
  • 1,348
  • 2
  • 11
  • 21
5
votes
3 answers

useHistory react-router-dom invalid hook call

I am trying to implement useHistory hook from react-router-dom and it doesn't seem to work. Throws invalid hook call App.js import React, { Component } from 'react'; <...> import { Signup } from './Signup.js'; import { Login } from…
devtom
  • 101
  • 4
5
votes
4 answers

React-router v6 window.scrollTo does not work

I am trying to fix the scrolling issue in React wherein when I was to scroll down to a certain point on the page, and then navigates to another page, that page will begin at the same scroll point as the previous page. Here's what I tried: import {…
fmsthird
  • 1,745
  • 2
  • 17
  • 34
1 2 3
99
100