This package exposes hooks to facilitate navigation and routing.
npm install @xtreamsrl/react-routingThis package exposes hooks to facilitate navigation and routing.
``shell`
npm install @xtreamsrl/react-routing
, Routes and Route straight from the react-router-dom library.$3
In React router, URL parameters are placeholders declared in a Route, like in the example below (_:first_ and _:second_ specified in the path field are placeholders).
Then it is possible to retrieve the route parameters in the ParamsConsumer component using the useParams hook.
`tsx
import {useParams} from "@xtreamsrl/react-routing";
import {BrowserRouter, Routes, Route} from "react-router-dom";function ParamsConsumer() {
const {first, second} = useParams<{first: string, second: string}>()
return
{first}, {second}
}export function App() {
return (
home
In the case of http://localhost:4200/a/b, first would be equal to 'a' and second to 'b'.$3
The hook simplifies retrieving and handling query parameters from URLs.
`tsx
function QueryParamsConsumer() {
const {first, second} = useQueryParams<{first: string, second: string}>()
return {first}, {second}
}export function App() {
return (
home
In http://localhost:4200/queryParams?second=test2&first=test1 the QueryParamsConsumer extracts first equal to 'test1' and second to 'test2'. $3
This hook facilitates navigation control. It returns four functions that can be used as needed:
- goBack
- goForward
- goBackOf: that allows to specify the number of pages to go back of
- goForwardOf: that allows to specify the number of pages to go forward of
`tsx
const navigation = useBrowserNavigation()return <>
>
``