M
M
Mirrrta2020-06-24 18:47:37
JavaScript
Mirrrta, 2020-06-24 18:47:37

How to correctly check the url for a match?

I check like this

const routes = {
  main: '/',
  news: '/news',
  staticNews: '/news/:id',
  page: '/pages/:id',
};

const location = useLocation();
Object.values(routes).find((route) => location.pathname === route)  // проверка на совпадение url

But the problem is that everything is ok with pages '/' and '/news', but how to correctly check for such pages '/news/:id'' and '/pages/:id'. And what about pages with such url '/news/:id/any'? Tell me please

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Ineshin, 2020-06-24
@Mirrta

And you don't have to do it manually. Such urls should be parsed by the router:
https://habr.com/ru/post/329996/
https://github.com/ReactTraining/react-router
and google further

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question