Custom components not being matched using react-router - javascript

I am trying to separate concerns with my Routes, keep my react code more organized. I am currently using react-router-dom v5.
I have an Application Routes component that has 3 children as components
AuthenticatedRoutes
PublicRoutes
Error404Route
Each component renders different routes/components, but only the first component (AuthenticatedRoutes) is being matched.
Application Routes
export const ApplicationRoutes = () => (
<Switch>
<AuthenticatedRoutes />
<PublicRoutes />
<Error404Route />
</Switch>
);
Authenticated Routes
export const AuthenticatedRoutes = () => (
<Switch>
<Route exact path='/dashboard'>
<Dashboard />
</Route>
<Route exact path='/profile'>
<Profile />
</Route>
</Switch>
);
Public Routes
export const PublicRoutes = () => (
<Switch>
<Route exact path='/about'>
<About />
</Route>
<Route exact path='/'>
<Home />
</Route>
</Switch>
);
Error Route
export const Error404Route = () => (
<Switch>
<Route>
<Error404 />
</Route>
</Switch>
);
So, was I was saying only the AuthenticatedRoutes (/dashboard and /profile) are being matched, the public routes and error404 route are not.
I thought that if you used a Switch the route will try to match the location pathname, if not, then the Error404Route will display.
Am I missing something? (sure I am)
Thanks!

Try it like this:
Application Routes
import AuthenticatedRoutes from './AuthenticatedRoutes'
import PublicRoutes from './PublicRoutes'
import Error404Route from './Error404Route'
export const ApplicationRoutes = () => (
<Switch>
{AuthenticatedRoutes}
{PublicRoutes}
{Error404Route}
</Switch>
);
Authenticated Routes
import Dashboard from './Dashboard'
import Profile from './Profile'
export const AuthenticatedRoutes = [
<Route exact path='/dashboard' component={Dashboard}/>,
<Route exact path='/profile' component={Profile}>,
];
Public Routes
import Home from './Home'
import About from './About'
export const PublicRoutes = [
<Route exact path='/' component={Home}/>,
<Route exact path='/about' component={About}>,
];
Error Routes
import Error404 from './Error404'
export const Error404Route = [
<Route exact path='/' component={Error404}/>,
];

Related

React router v6: catch all "*" path does not work when using nested routes

I have the following two files
AppRoutes.tsx
import { Route, Routes } from "react-router-dom";
import NotFound from "../pages/NotFound";
import MessageRoutes from "../features/messages/routes/MessageRoutes";
import Home from "../pages/Home";
export default function AppRoutes() {
return (
<Routes>
<Route path="/" element={<Home />} />
<Route path="/messages/*" element={<MessageRoutes />} />
<Route path="*" element={<NotFound />} />
</Routes>
);
}
MessageRoutes.tsx
import { Route, Routes } from "react-router-dom";
import ProtectedRoutes from "../../../routes/ProtectedRoutes";
import MessageOverview from "../pages/MessageOverview";
import NewMessage from "../pages/NewMessage";
export default function MessageRoutes() {
return (
<Routes>
<Route element={<ProtectedRoutes />}>
<Route path="/" element={<MessageOverview />} />
<Route path="/new" element={<NewMessage />} />
</Route>
</Routes>
);
}
Because I'm using the path "/messages/*" to capture all paths that start with /messages, my MessageRoutes component takes care of these nested routes. I have a final "*" route in the AppRoutes component to capture any url that the app does not support. But if the path would be "/messages/loremipsum", react router does not catch the NotFound route because everything that starts with "/messages" will be handled with the MessageRoutes component.
Does this mean that in every nested route component I now have to add a final <Route path="\*" element={\<NotFound /\>} /\> again, just to support a final catch all route? I don't like this approach. Is there no absolute final catch all for every route?
Does this mean that in every nested route component I now have to add
a final <Route path="\*" element={<NotFound />} \> again
Yes, absolutely. Each Routes component manages its own "scope" of routes for what it can match. For example if the current URL path is "/messages/loremipsum" the root Routes component matches the "/messages/*" and correctly renders the MessageRoutes component. The MessageRoutes component's Routes component then works on matching on the next path segment. Since there is no "*/loremipsum" route path you need another "catch-all" route to handle this.
The issue is that a Routes component isn't aware of what descendent routes any of its routes may possibly be rendering.
Example:
import { Route, Routes } from "react-router-dom";
import NotFound from "../pages/NotFound";
import MessageRoutes from "../features/messages/routes/MessageRoutes";
import Home from "../pages/Home";
export default function AppRoutes() {
return (
<Routes>
<Route path="/" element={<Home />} />
<Route path="/messages/*" element={<MessageRoutes />} />
<Route path="*" element={<NotFound />} />
</Routes>
);
}
import { Route, Routes } from "react-router-dom";
import ProtectedRoutes from "../../../routes/ProtectedRoutes";
import MessageOverview from "../pages/MessageOverview";
import NewMessage from "../pages/NewMessage";
import NotFound from "../pages/NotFound";
export default function MessageRoutes() {
return (
<Routes>
<Route element={<ProtectedRoutes />}>
<Route path="/" element={<MessageOverview />} />
<Route path="/new" element={<NewMessage />} />
<Route path="*" element={<NotFound />} />
</Route>
</Routes>
);
}
If you want to have a single "catch-all" route then you'll need to have a single routes configuration.
Example:
import { Route, Routes } from "react-router-dom";
import MessageOverview from "../pages/MessageOverview";
import NewMessage from "../pages/NewMessage";
import NotFound from "../pages/NotFound";
import Home from "../pages/Home";
export default function AppRoutes() {
return (
<Routes>
<Route path="/" element={<Home />} />
<Route element={<ProtectedRoutes />}>
<Route path="/messages">
<Route index element={<MessageOverview />} />
<Route path="new" element={<NewMessage />} />
</Route>
</Route>
<Route path="*" element={<NotFound />} />
</Routes>
);
}
Now when/if the URL path is "/messages/loremipsum" then this Routes component knows what nested routes it is rendering and can match and can correctly render NotFound.

No routes matches for nested routing

I have simple web page and i have trouble with routing:
Index.tsx:
const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement
);
root.render(
<React.StrictMode>
<HomePage />
</React.StrictMode>
);
Homepage:
const HomePage = () => {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Main/>}>
</Route>
<Route path="/login" element={<Login/>}>
</Route>
<Route path="/register" element={<Register/>}>
</Route>
</Routes>
</BrowserRouter>
)
};
export default HomePage;
This works as intended -> if user goes to 'www.example.com' Main component is rendered, if user goes to 'www.example.com/login Login` component is rendered and so on.
However, inside my Main component i also want to use routing:
const Main = () => {
return (
<div className="app">
<HomePageHeader/>
<Routes>
<Route path="/" element={<HomePageFeed/>}>
</Route>
<Route path="/animal" element={<AnimalSelection/>}>
</Route>
<Route path="/animal/:id" element={<Animal/>}>
</Route>
</Routes>
</div>
)
}
export default Main;
Main component has header for every content, however depending on the route i want to display different things, for / some default info, for /animal some menu and list of animals, and for /animal/:id info of specific animal.
However, when , inside HomePageFeed i try to redirect there:
let history = useNavigate(); history("/animal")
I get error: router.ts:11 No routes matched location "/animal"
What is correct way how to handle routing such as this? Is this corretly used routing?
Thanks for help!
My opinion is to have a single routing inside your application. Keep your routing part inside your App.js and share it in all your pages.
If you strictly want it to be in different component. Try wrapping the routes in the Main.tsx inside BrowserRouter might work. The problem might be because <Main /> component is not wrapped anywhere inside the BrowserRouter. Try like below,
const Main = () => {
return (
<div className="app">
<HomePageHeader/>
<BrowserRouter>
<Routes>
<Route path="/" element={<HomePageFeed/>}>
</Route>
<Route path="/animal" element={<AnimalSelection/>}>
</Route>
<Route path="/animal/:id" element={<Animal/>}>
</Route>
</Routes>
</BrowserRouter>
</div>
)
}
export default Main;

Using condition for redirecting in react router dom v6

I'm currently getting troubled with the react router dom#6. Like, I want to redirect users to other pages If they logged in. But when I use Navigate component It threw error saying that :
"[Navigate] is not a component. All component children of Routes must be a Route or React.Fragment"
Though I have wrapped it in the fragment but the result still the same. Please help me, this is my code:
import {BrowserRouter as Router, Routes, Route, Navigate} from 'react-router-dom';
import Home from './pages/Home';
import ProductList from './pages/ProductList';
import Product from './pages/Product';
import Register from './pages/Register';
import Login from './pages/Login';
import Cart from './pages/Cart';
import ErrorPage from './pages/ErrorPage';
function App() {
const user = true;
return (
<Router>
<Routes>
<Route path="/" element={<Home/>} />
<Route path="/products/:category" element={<ProductList/>} />
<Route path="/product/:id" element={<Product/>} />
<Route path="/cart" element={<Cart/>} />
<Route path="/dang-ky" element={<Register/>} />
<Route path="/dang-nhap" element={<Login/>} >
{user ? <><Navigate to="/" replace/></> : <Login/>}
</Route>
<Route path="*" element={<ErrorPage/>} />
</Routes>
</Router>
);
}
export default App;
You can simply use Navigate component from 'react-router-dom' library and use it to redirect inside the element attribute of the '/login' route.
import { BrowserRouter as Router, Routes, Route, Navigate } from "react-router-dom";
<Route path="login" element={user ? <Navigate to="/"/> : <Login />} /> {/* If user is logged in, then redirect to home page, else go to login page */}
Redirecting users if logged in, necessarily does not need a navigation, you could simply just specify the element you want to reach eg:
<Route exact path="/" element={user ? <Home/> : <Login />}/>
Edit:
This is also a possible solution, drawback is that you cant redirect properly. You just have to make sure that you have the fallbackroutes set up right.
{isSignedIn && <Route path="" element={} />}
I would recommend using an AuthRout to wrap your Routes.
function AuthRoute ({children}) {
if(!user.isSignedIn){
//Not signed in
return <Navigate to="/signIn" />
}
//Signed in
return children
}
//Route itself
<Route path="/abc" element={<AuthRoute><YourComponent /></AuthRoute>} />
That way the user gets redirected if he is not signed in
sorry for the that problem, but you can use useNavigate from react-router-dom v6
as below
import { useNavigate } from 'react-router-dom';
export const MyComponent = ({user}) => {
const navigate = useNavigate();
useEffect(() => {
if(user){
navigate('/dashboard')
}else{
navigate('/login')
}
......
},[])
.....
}
and use that codes on your route logic, it makes your route more clean
<Router>
<Routes>
<Route path="/" element={<Home/>} />
<Route path="/products/:category" element={<ProductList/>} />
<Route path="/product/:id" element={<Product/>} />
<Route path="/cart" element={<Cart/>} />
<Route path="/dang-ky" element={<Register/>} />
<Route path="/dang-nhap" element={<MyComponent user = {user} />}/>
<Route path="*" element={<ErrorPage/>} />
</Routes>
</Router>

Re-use navbar component with multiple components

I have a simple question. What is the best way to use a navbar with multiple components using react router? Just let me show the code so you can understand what I'm trying to say.
import React from "react";
import "./App.css";
import {
BrowserRouter as Router,
Switch,
Route,
Redirect
} from "react-router-dom";
import Auth from "./website/Auth/Auth";
import SocialMedia from "./website/SocialMedia/SocialMedia";
import SingleUser from "./website/SingleUser/SingleUser";
import Search from "./website/Search/Search";
import SinglePhoto from "./website/SinglePhoto/SinglePhoto";
import Navbar from "./components/Navbar/Navbar";
function App() {
const logIn = JSON.parse(localStorage.getItem("token"));
return (
<Router>
<Switch>
<Route exact path="/" component={Auth}>
{logIn ? <Redirect to={`/profile/${logIn.data.id}`} /> : <Auth />}
</Route>
<Navbar>
<Route exact path="/profile/:id" component={SingleUser} />
<Route exact path="/socialmedia" component={SocialMedia} />
<Route exact path="socialmedia/search" component={Search} />
<Route exact path="socialmedia/photo/:id" component={SinglePhoto} />
</Navbar>
</Switch>
</Router>
);
}
export default App;
So I have to reuse my Navbar component, and I tried to use <Navbar />, then the other routes below, but that wasn't working, and when I put <Navbar> </Navbar> that worked and the other components will appear, but is that the way I reuse my Navbar component?
Thanks for your time !!
import React from "react";
import "./App.css";
import {
BrowserRouter as Router,
Switch,
Route,
Redirect
} from "react-router-dom";
import Auth from "./website/Auth/Auth";
import SocialMedia from "./website/SocialMedia/SocialMedia";
import SingleUser from "./website/SingleUser/SingleUser";
import Search from "./website/Search/Search";
import SinglePhoto from "./website/SinglePhoto/SinglePhoto";
import Navbar from "./components/Navbar/Navbar";
function App() {
const logIn = JSON.parse(localStorage.getItem("token"));
return (
<Router>
<Switch>
<Route exact path="/" component={Auth}>
{logIn ? <Redirect to={`/profile/${logIn.data.id}`} /> : <Auth />}
</Route>
<Route Component={Navbar}>
<Route exact path="/profile/:id" component={SingleUser} />
<Route exact path="/socialmedia" component={SocialMedia} />
<Route exact path="socialmedia/search" component={Search} />
<Route exact path="socialmedia/photo/:id" component={SinglePhoto} />
</Route>
</Switch>
</Router>
);
}
export default App;
Try this!
If you want the Navbar to render only on certain routes then render it only on certain routes. Render the Navbar into a route outside the Switch and specify all the paths you want it to be rendered on in an array on the path prop.
Additional notes:
Within the Switch component, order and path specificity matter, reorder your routes to specify more specific paths before less specific paths. This allows you to not need to specify the exact prop for every route.
Don't specify both a component prop and render children on a single Route, see Route render methods. Just render the Redirect or Auth component as children.
Code:
function App() {
const logIn = JSON.parse(localStorage.getItem("token"));
return (
<Router>
<Route
path={["/profile", "/socialmedia"]}
component={Navbar}
/>
<Switch>
<Route path="/profile/:id" component={SingleUser} />
<Route path="socialmedia/photo/:id" component={SinglePhoto} />
<Route path="socialmedia/search" component={Search} />
<Route path="/socialmedia" component={SocialMedia} />
<Route path="/">
{logIn ? <Redirect to={`/profile/${logIn.data.id}`} /> : <Auth />}
</Route>
</Switch>
</Router>
);
}

React Router 4 Nested Routes not in Props [duplicate]

I've upgraded the react router to version 4 in my application. But now I'm getting the error
Warning: You should not use <Route component> and <Route children> in the same route; <Route children> will be ignored
What is wrong with this routing?
import {
Switch,
BrowserRouter as Router,
Route, IndexRoute, Redirect,
browserHistory
} from 'react-router-dom'
render((
<Router history={ browserHistory }>
<Switch>
<Route path='/' component={ Main }>
<IndexRoute component={ Search } />
<Route path='cars/:id' component={ Cars } />
<Route path='vegetables/:id' component={ Vegetables } />
</Route>
<Redirect from='*' to='/' />
</Switch>
</Router>
), document.getElementById('main'))
IndexRoute and browserHistory are not available in the latest version, also Routes do not accept children Routes with v4, Instead, you can specify Routes within the component Itself
import {
Switch,
BrowserRouter as Router,
Route, Redirect
} from 'react-router-dom'
render((
<Router>
<Switch>
<Route exact path='/' component={ Main }/>
<Redirect from='*' to='/' />
</Switch>
</Router>
), document.getElementById('main'))
Then in the Main Component
render() {
const {match} = this.props;
return (
<div>
{/* other things*/}
<Route exact path="/" component={ Search } />
<Route path={`${match.path}cars/:id`} component={ Cars } />
</div>
)
}
Similarly in the cars component
you will have
render() {
const {match} = this.props;
return (
<div>
{/* other things*/}
<Route path={`${match.path}/vegetables/:id`} component={ Vegetables } />
</div>
)
}
Nested routes are not available from version react-router 4.x. Here is a basic example straight from react-router documentation on how to code for nesting route secnarios in v4.x.
Also have a look on this question as well Why can I not nest Route components in react-router 4.x?

Categories