0

I don't find the way ton fix my issue. I want a specific state if the route is '/blablabla/:id'. I know how to find the simple route as '/blabla'. With a if statement is ok but i need to use switch case. Thanks for your help

 useEffect(() => {
    switch (location.pathname) {
      case '/':
        setTitle('My Board');
        break;
      case '/offers':
        setTitle('My Offer');
        break;
      case '/invoice':
        setTitle('Invoices');
        break;
      case location.pathname.match(/\d+$/): -- The route is /invoice/:id --> the id is a number 
        setTitle('Invoice Number ***');
        break;
      default:
        setTitle('My Board');
    }
  }, [location]);
Desdamo
  • 59
  • 1
  • 6

1 Answers1

0

Your regex just checks for numbers but not the characters before it. This checks for characters followed by a slash and numbers.

^([a-z]+\/[0-9]+)
sid
  • 1,779
  • 8
  • 10
  • yes right but its strange it like if the regex doesnt match like this... – Desdamo Oct 26 '21 at 08:25
  • any error or something? maybe try hardcoding and see if it works. if not, then you're digging up the wrong hole :D – sid Oct 26 '21 at 08:40
  • so i find the right way : switch (true) { .... case /\d+$/.test(location.pathname): but how to regex a pathname like this : /account/sample-page because the - is a problem wiith regex ?? – Desdamo Oct 26 '21 at 09:01