Converting HTML links to SPA route links
A chunk of static HTML content is an example where the route link component can't be directly used but it still might be desirable to make plain HTML links in that content behave as SPA route links. The useRouteLinks()
hook can be helpful here:
import {useRef} from 'react';
import {useRouteLinks} from 'routescape';
let Content = ({value}) => {
let containerRef = useRef(null);
useRouteLinks(containerRef);
return (
<div ref={containerRef}>
{value}
</div>
);
};
In this example, the useRouteLinks()
hook makes all HTML links inside the container referenced by containerRef
act as SPA route links.
To be more specific as to which elements in the container should be converted to route links, a selector, or an HTML element, or a collection thereof, can be passed as the second parameter of useRouteLinks()
:
useRouteLinks(containerRef, '.content a');
←
Routing middleware
|
<Router>
→