Questions tagged [react-router-dom]

If you have questions about utilizing the DOM and setting up bindings in React Router versions 4, 5, or 6, use this tag.

What are some methods for utilizing the data received through this.props.history?

Whenever I need to navigate from one route to another using this.props.history.push("/"), I also want to pass along some extra data. For example: this.props.history.push('/postDetail', {data: item}). However, I am uncertain about where to define ...

The React loader fails to function properly when used with nested routes

I'm currently working on my App.js file where I have defined all the routes for my application. I wanted to implement React-Router data loader functionality. import React from 'react' import { Routes, Route, Navigate, RouterProvider, createB ...

Is there a React equivalent to window.location.replace()?

For example, here is the code I use for my login button: var rJson = JSON.parse(request.responseText); if (rJson.success) { console.log("login success"); window.location.replace(redirectLink); } While it wor ...

The cartItems app was unable to find any matching routes for the specified location

Encountering a routing issue while using react-router-dom v6 to manage the functionality of "cart items" in my application. Strangely, the cart items fail to show up when clicking on the "cart" link on the navbar header. However, it functions correctly and ...

What is the best way to stop event bubbling in react-router-dom when using the <Link> component?

Hey there! I have a situation that I need help with. I tried putting the Link around the whole post so that I could prevent bubbling for individual buttons, but for some reason stopPropagation() is not working as intended. Following advice from another s ...

The transition effect is not activated when using the MUI Drawer with react-router-dom

My goal was to merge the persistent drawer similar to the example on the MUI website with react-router-dom. However, I encountered a problem where the transition effect no longer seems to work properly. The paragraph does not resize to accommodate the open ...

Tips for successfully passing a function to a link component in React Router v6

I'm facing an issue with passing a function to a link component. I tried using state and useLocation, which work fine for regular variables, but useLocation returns null when attempting to pass down a function. const myFunction = someFunction(); <Link ...

Is the useNavigate() function failing to work consistently?

Currently facing an issue while working on a MERN Web App. When logging in with an existing account, the backend API call returns user properties and a JWT Token. Upon saving it, I use the navigate function to redirect the User to the homepage. Everything ...

React Router Dom 6+ is causing an error when attempting to import 'withRouter' as it is not exported from the 'react-router-dom' package

Whenever I attempt to execute the command '[email protected]' I encounter Error message: 'withRouter' is not available for export from 'react-router-dom' import {Route, withRouter } from "react-router-dom"; ...

Having issues with the link page in react-router-dom?

Currently, I am utilizing the react-router-dom library to manage routers. However, I have encountered an issue where linking to another page does not change the page, but instead adds the new page right after the previous one as shown in the image below. W ...

React JS - Breaking down the distinction between PublicTheme and PublicTheme

In my React project, I am currently working on creating the admin dashboard and designing the UI area for user interaction. I have encountered an issue where I am unable to separate the admin theme from the PublicTheme. Even when navigating to "/admin/lo ...

Alternative for using useRouteMatch to retrieve parameters

I'm currently refactoring this code and struggling to find a suitable replacement for this section. This is due to the react-router-dom v6 no longer having the useRouteMatch feature, which I relied on to extract parameters from the URL: import React, ...

The Protected Routes in React Router Dom persistently redirecting

I am currently implementing protected routes in my Redux and Pure React app using React Router Dom. The issue I am facing is that when I navigate to /profile/edit and then refresh the page, it automatically redirects me to /login and then to /profile. I ha ...

When directed to a different page, Fetch does not activate

Having trouble getting the fetch function to run more than once in my application. It works the first time, loading the page with received data, but when I navigate to a new URL without refreshing the page, nothing changes - not even the state. The same is ...

Learn how to utilize react-router for re-rendering components in React with this simple example. Earn +100 reputation

I am new to React, especially with react-router. I came across an example on this website: . I decided to try replicating it on Codesandbox.io as well: https://codesandbox.io/s/7jxq0j6qp0 My issue is that the Menu component keeps re-rendering every time ...

Router DOM in conjunction with Next.js

I am attempting to extract the output of the code in navigation, but unfortunately it is generating a dreadful error: Unhandled Runtime Error Error: You cannot render a <Router> inside another <Router>. You should never have more than one in ...

Having trouble getting react router to function properly with multi-layer routes containing parameters

I have been working on configuring my react-router and so far it has been smooth sailing with simple routes like: /login, /logout, /admin. However, I am now facing an issue with setting up a route like this: /admin/groups/modify/:groupID. While the /admin/ ...

I'm having trouble figuring out why this React Router setup is not functioning properly. Can anyone provide any insights

As I delve into react routing practice, I've put together a geography-based web app. Starting off, I configured the router paths: import { StrictMode } from "react"; import { createRoot } from "react-dom/client"; import { BrowserRouter, ...

`When both routes are combined, a reaction occurs.`

After creating two routes, I'm facing an issue where the 'Main' component is displaying properly but a 'NotFound' page also appears at the bottom. Any suggestions on how to fix this? I've spent the last 10 hours trying to troubleshoot this problem, but ha ...

The useNavigate() hook from react-router-dom is not properly setting the id in the URL path

I am currently using react-router-dom v6 for my routing needs. My goal is to pass an ID in the navigate URL path. Here is the onClick method and button code that I am working with: let navigate = useNavigate(); const routeChange = (id) => { let ...

The Private Route feature is failing to display the About Component

I have my private routes set up in App.js, and all the other components within the private routes render correctly except for the About Component. Initially, I was receiving an error message stating something like "expected a string but got an object." N ...

The young one emerges within the SecurePath component temporarily

Setting up authorization in React has been a priority for me. Ensuring that users cannot access unauthorized pages within the application is crucial. To achieve this, I have created a custom component as shown below. import { ReactNode } from "react&q ...

When clicking on the CardActionArea component from Material UI, the React Component fails to render

Encountering a unique problem I've never faced before. Utilizing Material UI components, specifically using CardActionArea in conjunction with Redirect from react-router-dom When the user clicks on the CardActionArea, the intention is to direct them to ...

Using React Router version 5 along with history but encountering a problem where no content is displayed during

Struggling to figure out what I'm missing here. My routing was functioning well until I switched from BrowserRouter: import { BrowserRouter as Router } from 'react-router-dom' Now, I need to navigate programmatically outside of my componen ...

Creating clickable links to MySQL data in Reactjs frontend

Required feature: The project names fetched from the MySQL database and displayed in a table column should serve as links leading to the corresponding information on the /projects page. Current status: Unfortunately, the project names extracted from the M ...

Utilizing numerical values in useParams - A beginner's guide

Trying to access specific data from my json file using an ID, like "http://localhost:3001/pokemons/3", leads to a 404 error. All the data is visible at http://localhost:3001/pokemons. It seems that useParams doesn't want me to use id as a number - quite ...

After entering a query in the search bar, proceed to the search results page and continue using the same search bar on the results page

Within my React application, I have implemented a layout that includes a sidebar, header with profile section and search bar components. The header is displayed on every page or route, allowing users to perform searches from any location. Upon initiating a ...

Steps for updating react-router-dom to version 6

Convert all Switch elements to Routes and components to element How can I update the remaining parts to v6? Routes.js import React, { lazy, Suspense } from 'react'; import { HashRouter as Routes, Route, Router } from 'react-router-dom&apo ...

In React Router v6, you can now include a custom parameter in createBrowserRouter

Hey there! I'm currently diving into react router v6 and struggling to add custom params in the route object. Unfortunately, I haven't been able to find any examples of how to do it. const AdminRoutes: FunctionComponent = () => { const ...

React Router: Displaying a Sidebar on the Login Page

I'm trying to figure out how to hide my sidebar on the login page so only logged-in users can see it. Here's my code, but I can't seem to hide the sidebar... <BrowserRouter> <div className="container"> ...

At the rendering of App.js, React and Redux collaborate to dispatch an action three times

I am encountering an issue with this section of code while utilizing Redux for state management. The expected behavior is to trigger the dispatcher (noted with a comment) only once when the user logs in, but it is being called three times every time App. ...

Having trouble with React Router 4 in a subfolder?

import { BrowserRouter as Router, Route } from 'react-router-dom' <Router> <switch> <Route exact path='/' component={main} /> <Route path='/build/signup' component={SignUp} /> </swit ...

React Router Issue: Component Not Rendering When <nav> Element Is Incomplete

I am currently experiencing an issue with rendering a component in my React TypeScript application using React Router. The problem arises when trying to navigate to the AddTask component by clicking on a link within a <nav> element. Strangely, the co ...

Finding the current URL in React Router can be achieved by using specific methods and properties provided by

Currently, I'm diving into the world of react-redux with react-router. On one of my pages, it's crucial to know which page the user clicked on to be directed to this new page. Is there a method within react-router that allows me to access inform ...

Issue with React Routes only occurring in the production website

I'm encountering an issue on my personal website that only occurs in production, but not in my local environment. Here's the situation: I have set up the routes as follows const Routes = () => ( <Router> <Route exact path=&quo ...

Issue with React routing: When passing id params in the URL using history.push, it is returning undefined

Struggling with using history.push in an onClick event to pass user id to a Profile page component, but the uuid params in the URL are showing up as undefined. Feeling stuck at this point. I also need to include all other props obtained from the Random Us ...

Leverage URL parameters as a prop within a React component

I am grappling with setting up an onLoad event in order to retrieve data from my database. My challenge lies in utilizing a URL parameter as a prop for this task, and I seem to be stuck on how to access it. Currently, I am working with react-router v6, ev ...

What are the steps to ensure compatibility with relative paths when transitioning from v5 to v6?

In my application, there are scenarios where multiple routes must pass through a component before rendering specifics. Additionally, there are situations where something is displayed for the parent route and then divided for the children. It's crucial to ...

How can you apply an active class using React-Router?

My React-Router navigation issue nav.tsx import React from 'react' import { menu } from './menu' import { Link } from 'react-router-dom' import styles from './HamburgerMenu.module.scss' const HamburgerMenu: React.F ...

Having trouble passing props through the variable path /:id in React Router DOM

The console.log confirms that the user input 'searchTerm' is successfully raised up from the component by the onClick function and then saved to the state in the handleClick function. However, it does not pass through to the second route. fun ...

Using React, a link to the same component is created, but a subcomponent is mistakenly using an outdated version of

Here, we have a SubComponent and a MainComponent created to showcase an image collection. The Subcomponent allows you to toggle between pictures in the collection using the onclick() event. The MainComponent also includes links to other collections, which ...

Error: The 'Storage' variable has not been defined

Just starting out with React and trying to learn from various online resources. I stumbled upon some MERN full stack login code on the internet. When I run npm start, everything works fine, but when I try npm test, I get an error saying 'ReferenceError: lo ...

Do not trigger navigation on click for the element enclosed within <Link></Link> tags

Is it possible to implement a functionality where clicking on a div, wrapped in react-router-dom tags, will navigate to the established path, but if the click target is a delete button within that div, the navigation should be bypassed and only the onCli ...

Guiding the user to a different React page after a successful login: a simple solution

Currently, I am working on developing my very first full-stack application with React for front-end and Node.js with Express for back-end. I have set up a /login route using react router dom where users can input their email and password ...

What is the process for rendering a React class component using React HashRouter and Apollo client?

I've run into an issue with my project that involves using only React class components and fetching data from an Apollo server. The problem I'm facing is that, in Chrome, only the Navbar.jsx component is rendering. Even when I navigate to one of the link ...

Unable to find custom components when using react-router

My goal is to improve the organization of my Routes in React and separate concerns. I am currently utilizing react-router-dom version 5. Within my Application Routes component, I have structured it with 3 children components: AuthenticatedRoutes PublicRo ...

Issue: Assertion violation: The use of <Link> element is restricted to within a <Router>. Any solutions or suggestions?

In my React and Next Js application, I am utilizing react-router-dom for routing purposes. The dependencies listed in the package.json file are as follows: This is how my current package.json file looks: { "name": "MUSIC", "versio ...

Avoid using the Router with the Search component in React JS

Having trouble rendering my Search component within the main Header component using react-router-dom. I suspect there's an issue with this line of code <Route render={({ history }) => } /> I've been stuck on this for two days now... T ...

Issue with react router v6: Component fails to render even though route has been changed

The router seems to be experiencing an issue where it does not render a component. Specifically, on the home page, the Private Route is only rendered once. Clicking on a NavLink changes the URL to "/agreements", but the component itself is not being render ...

The history.push method in React seems to be malfunctioning, and the useHistory hook

I am struggling to implement a page redirection upon form submission. I have come across various suggestions, such as: this.props.history.push("/") And also the recommendation to utilize: import { useHistory } from 'react-router-dom'; //Followe ...

No visual changes with Material UI Switch

I have implemented Material UI's switch component in my multi-page app to change themes. However, I encountered a bug where the switch does not visually toggle from left to right (UI functionality is not working), even though the onChange event handler fun ...

Dividing React Router into individual files

I am facing an issue while trying to implement routes in my react project with three separate components: Window.js import React, { Component } from 'react'; import SideBar from "../SideBar/SideBar"; import MainBody from "../MainBody/MainBody"; import { ...

What is the best way to navigate from a Laravel Blade view to a different page based on React Router?

I am currently using react in combination with laravel, where my view routes are dependent on the react router. In order to achieve this, I have added the following Route in web.php: Route::get('/{path?}',function(){return view('welcome');}); along with o ...

The best way to organize routes in react.js is by incorporating nested routes and a private route component for added security

I have been working with react-router-dom and my app.js file currently has routing set up like this: <BrowserRouter> <div className="App container"> <Header/> <Switch> <Route exact path='/& ...

Utilizing React Router to dynamically render components based on JSON object data

My current challenge involves rendering React components based on the component names in a JSON file I've created. Specifically, I want to render the TestSection component within the context of my Route component. To achieve this, I am utilizing the ...

Trouble with Nested Routing in React Router Version 6: Route not Rendering

I'm facing issues nesting a path within another path in react-router-dom version 6. When I try to visit the nested argument's page (/blog/1), it shows up as a blank non-styled HTML page. However, when I add a child path to the root like /blog, it ...

Having trouble establishing a default route with React Router v5

I am facing an issue with setting the default route to the home page in react router v5. Despite trying several methods, I cannot get it to work as expected. Index.js import React from "react"; import ReactDOM from "react-dom"; import ...

Switching sub components based on routing through navigation links after logging in

I'm having an issue with my routing setup while transitioning from the login page to the main page. Here's how my Routes are structured: App.jsx <BrowserRouter> <Routes> <Route path="/main/" element={<Main />} /& ...

Is there a way to modify the color of a Link component in React using Material-UI library?

I've encountered a problem trying to change the color of a Link inside a Button. Despite setting the secondary color for the Button, the color change doesn't seem to take effect, whereas it works fine for other components. <AppBar position=&a ...

What is the best way to define a custom route in react-router-dom?

My goal is to have the URL display "/login" in the address bar when I am on the login page. // App.js <Routes> {isLoggedIn ? ( <Route path="/" element={<Root onLogout={handleLogout} />}> <Route index e ...

The React App deployed on the S3 bucket is only displaying the homepage, and it seems like the Hash router is

After deploying my react app on an S3 bucket, I encountered an issue where only the home page would display and all other pages would result in a 404 error. Despite switching from browserHistory to hashHistory, the problem persisted. The console showed a ...

Ways to add a React Router Link to a Material UI TableRow

When attempting to incorporate a Link component from React Router Dom into my Material UI TableRow, I encountered an issue. <TableRow component={Link as any} to={`/company/${company.id}`} className="clt-row" key={company.id}> The error message I re ...

Enhancing parent component props in React-router-dom: A guide to updating them

Here is the structure of my App component: const App = (props) => ( <BrowserRouter> <PageTheme {...some props I'd like to change on route change}> <Switch> <Route exact path="/example"> <E ...

Struggling to implement nested routes with react-router-dom version 5.2.0?

I'm currently working on implementing nested routing in React using react-router-dom 5.2.0. For a better understanding of the project, you can access the CodeSandbox link here: https://codesandbox.io/s/nested-routes-8c7wq?file=/src/App.js Let's start by l ...

React Navigation Bar Links Fail to Show Content

I'm a beginner in the world of React and I could really use some assistance. Right now, I am working on creating a portfolio using React and focusing on the Nav component. Although I haven't encountered any errors in the Console, when I click on each Link ...

Issue concerning the implementation of <Routes>, <Route> and <Navigate> in react-router-dom version 6

I'm feeling a bit lost when it comes to the new usage of react-router-dom v6. According to their documentation, we should be able to simply place our Route component inside the element prop, but I keep encountering this error Uncaught TypeError: meta.relat ...

encountering an issue with the react hook useHistory leading to an error

I recently encountered an issue while implementing useHistory() in my code. Previously, I had used it without any errors, but now I'm getting the following error in this component: Line 6:18: React Hook "useHistory" is called in function "showPost" which ...

Unable to successfully redirect to the homepage following a login with the <Redirect> feature

I am trying to implement a Redirect from react-router-dom version 4.3.1. I have all the logic in my app.js file where I make a request for the token, and if it exists, I need to redirect the user to the homepage. I also attempted to use history, but it doe ...

What is the process for appending a component to window.location.href?

I am working on implementing unique custom URLs for each item based on user interaction. After a user clicks "View Item", I want to redirect them to a URL that includes the item's name and ID, like this: "http://localhost:3000/item-1/-MC_xbIMm8zctEWRJ-Lj/" ...

Preventing navigation without using the <Prompt> component in React Router DOM V6

I have recently discovered that in react-router-dom v5, it was possible to utilize the <Prompt> component to prompt the user before a page transition occurs, allowing them to prevent it. However, this feature has been removed in v6 with plans for a m ...

Utilize react-router-dom for conditional rendering based on button clicks

When the user types in "user" in the text box, they will be directed to the user page. If they type "admin", they will be redirected to the admin page. This code belongs to me. constructor(props) { super(props); this.state = { userType : 0 ...

Encountering errors while setting up routes with Browser Router

When setting up a BrowserRouter in index.tsx, the following code is used: import './index.css'; import {Route, Router} from '@mui/icons-material'; import {createTheme, ThemeProvider} from '@mui/material'; import App from './App'; import {StrictMode} from ...

Is there a way to display the drawer component from Material UI only on specific routes using routing in ReactJS with MaterialUI?

In my react project, I have implemented a material-UI drawer component. The issue I am facing is that the drawer component contains the page content within itself. Previously, I managed to integrate routes using react-router-dom with the drawer. My current ...

What is the best way to add conditional styling to the home page item button in a React application?

Utilizing react alongside Material UI to display a navigation bar, I am attempting to dynamically apply styles to the selected menu item based on the URL. To achieve this, I make use of useLocation hook to retrieve the current URL: import { Link, useLocati ...

Experiencing difficulty in finding my way to other web pages

I've developed a React website with user authentication and a sidebar. The routes are protected to ensure that only logged-in users can access certain pages. To show the sidebar only after login, I have placed it inside protected routes and kept sign- ...