question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamic route doesn't work in 4.0

See original GitHub issue

routes.js

const routes = [
  { path: "/", exact: true, name: "Home" },
  { path: "/product/list", name: "ProductList", component: ProductList },
  { path: "/product/detail/:id", name: "ProductDetail", component: ProductDetail }
];

when I enter /product/detail/2, It throws Uncaught TypeError: Cannot read property ‘name’ of undefined.

AppGreadcrumb.js

const getRouteName = (pathname, routes) => {
    const currentRoute = routes.find((route) => route.path === pathname);
    return currentRoute.name;
  };

I guess that ‘/product/detail/:id’ is not same as '/product/detail/2, so currentRoute return undefined and getRouteName function throw error.

Thanks.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
nicolereneecommented, Aug 13, 2021

I was able to fix this by changing the getRouteName to:

const getRouteName = (pathname, routes) => {
    const currentRoute = routes.find(
      (route) =>
        matchPath(pathname, {
          path: route.path,
          exact: route.exact,
          strict: route.strict,
        }) != null,
    )
    return currentRoute.name
  }

You will also need to add the following import:

import { matchPath } from 'react-router'
0reactions
Prasaa84commented, Jul 17, 2022

Hi… how to make this in react 18. This code is not working on it. Browser console says ’ No routes matched location “/admin/sometexthere” ’

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic route not working in build version of my react app
The following route is dynamic and doesn't work. However, my guess is that it is not due to ":processFlowItemId" but instead due to ......
Read more >
Basics → Routing - Vapor Docs
To understand how routing works in Vapor, you should first understand a few basics ... based on the HTTP method and path, let's...
Read more >
Troubleshooting | Cloud Router
Use the following guide to troubleshoot common issues with Cloud Router: Configuration issues related to configuring and establishing BGP sessions.
Read more >
Routes' Matching Syntax - Vue Router
Routes' Matching Syntax #. Watch a free video lesson on Vue School. Most applications will use static routes like /about and dynamic routes...
Read more >
Specifying a Route's Model - Ember Guides
Sometimes you need to fetch a model, but your route doesn't have the parameters, because it's a child route and the route directly...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found