I am trying to create a navbar component which changes the link on a specific button based on the current URL.
This is what I have so far: Navbar.js
import React, { useState } from "react";
const testfun = () => {
        if (/\/test\/[0-9]+/.test(window.location.href)) {
            let id;
            const match = /(?:\/)(\d+)(?:\/)?/g.exec(window.location.href);
            console.log(match);
            if (match) {
                id = "/" + match[1] + "/edit";
            } else {
                id = "/test";
            }
            return (
                <Button component={Link} to={`/test${id}`} color="inherit">
                    Test2
                </Button>
            );
        } else {
            return (
                <Button component={Link} to="/test" color="inherit">
                    Test2
                </Button>
            );
        }
    };
export default function Navbar() {
    return (
        <div>
            <Button component={Link} to="/" color="inherit">
                            Home
            </Button>
            <Button component={Link} to="/test" color="inherit">
                            test
            </Button>
            {
               testfun ()
            }
    )
}
App.js
import React from "react";
import "./App.css";
import Navbar from "./components/Navbar";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
function App() {
    return (
        <Router>
            <Navbar />
            <div className="container">
                <Switch>
                    //Different routes
                </Switch>
            </div>
        </Router>
           )
}
export default App;
The idea is the following - if the user is on /test/123, where 123 is any number page and clicks the button Test2 which is rendered by the testfun function, it should get redirected to the /test/123/edit page. If the user is on any other page and clicks the same button, it should get redirected to /test page.
The implementation above renders the button Test2 but it always points to /test regardless of the page the user is on.
