React router v6 how to use `navigate` redirection in axios interceptor

In the pre-RRDv6 world you would create a custom history object, to be exported and imported and passed to a Router, and imported and accessible in external javascript logic, like redux-thunks, axios utilities, etc. To replicate this in RRDv6 you need to also create a custom router component so it can be passed an external … Read more

React Router Dom v6 shows active for index as well as other subroutes

You can specify the end prop on the “root”-level link to the “/dashboard” path. NavLink If the end prop is used, it will ensure this component isn’t matched as “active” when its descendant paths are matched. <NavLink to=” end> {({ isActive }) => isActive ? ( <text style={{color: blue}}>Home</text> ) : ( <text>Home</text> ) } … Read more

Uncaught TypeError: Cannot read property ‘push’ of undefined (React-Router-Dom)

In order to make use of history in the App component use it with withRouter. You need to make use of withRouter only when your component is not receiving the Router props, This may happen in cases when your component is a nested child of a component rendered by the Router or you haven’t passed … Read more

How do you pass data when using the navigate function in react router v6

It’s similar to how it’s done in v4, two arguments, the second being an object with a state property. navigate( ‘thepath’, { state: { //…values } } }) From the migration guide: Use navigate instead of history If you need to replace the current location instead of push a new one onto the history stack, … Read more

React Router not working with Github Pages

If deploying to GitHub, ensure there is a “homepage” entry for where you are hosting it in Github. Example: “homepage”: “https://github.com/amodhakal/portfolio”, Switch to the HashRouter since GitHub pages doesn’t support the tech used by the BrowserRouter. index import React from ‘react’; import ReactDOM from ‘react-dom/client’; import { HashRouter } from ‘react-router-dom’ import App from ‘./App’; … Read more

Detect Route Change with react-router

You can make use of history.listen() function when trying to detect the route change. Considering you are using react-router v4, wrap your component with withRouter HOC to get access to the history prop. history.listen() returns an unlisten function. You’d use this to unregister from listening. You can configure your routes like index.js ReactDOM.render( <BrowserRouter> <AppContainer> … Read more

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

I ran into the same issue today and came up with the following solution based on this very helpful article by Andrew Luca In PrivateRoute.js: import React from ‘react’; import { Navigate, Outlet } from ‘react-router-dom’; const PrivateRoute = () => { const auth = null; // determine if authorized, from context or however you’re … Read more

Error “Error: A is only ever to be used as the child of element”

Yes, in react-router-dom version 6 it is a bit different. Please look as the sample below. React Router tutorial import { render } from “react-dom”; import { BrowserRouter, Routes, Route } from “react-router-dom”; import App from “./App”; import Expenses from “./routes/expenses”; import Invoices from “./routes/invoices”; const rootElement = document.getElementById(“root”); render( <BrowserRouter> <Routes> <Route path=”/” element={<App … Read more