I'm trying to figure out custom react-router navigation when the enclosing BrowserRouter component defines a basename property.
I am using the Link component property to render a link as a higher-order Button component (in this case from the Kendo UI library). The onClick reads the href prop to push navigation via history. However, the resulting URL repeats the base pathname.
In this example (React 17, router 5.2), if you navigate to /area/simple and click the Link (which has no child component), it takes you back to /area, but if you navigate to /area/complex and click the Button, it takes you to /area/area. (Inspecting prop.href shows that it is /area.)
Is history push the wrong way to navigate here? How does the anchor-based Link manage to navigate correctly?
// imports omitted, Root is like App, it's a single-spa convention
const Root = () => {
return (
<Router basename="/area">
<Route exact path="/">
<h2>You Are Home</h2>
</Route>
<Route exact path="/simple">
<h2>Plain Link Component</h2>
<Link to="/">Go Home</Link>
</Route>
<Route exact path="/complex">
<h2>Link with Child</h2>
<Link to="/" component={ HomeButton }>
</Route>
</Router>
);
};
const HomeButton = (props) => {
return <LinkButton icon="home" {...props}>Home</LinkButton>
};
// normally a reusable component in a separate file
const LinkButton = (props) => {
const { push } = useHistory();
return (
<Button onClick={() => push(props.href)} {...props}>{props.children}</Button>
)
};
export default Root;
(I'm not interested in suggestions to re-style the Link as a button -- we're trying to stick to the built-in themed styles from the Kendo UI library, and replicating those accurately creates future maintenance headaches.)