T8 React Router
URL parameters
URL parameters, as a portion of the app's state, can be managed in the React's useState()-like manner, allowing for quick migration from local state to URL parameters or the other way around:
+ import { useRouteState } from "@t8/react-router";
let App = () => {
- let [{ coords }, setState] = useState({ coords: { x: 0, y: 0 } });
+ let [{ query }, setState] = useRouteState("/");
let setPosition = () => {
setState(state => ({
...state,
- coords: {
+ query: {
x: Math.random(),
y: Math.random(),
},
});
};
return (
<>
<h1>Shape</h1>
- <Shape x={coords.x} y={coords.y}/>
+ <Shape x={query.x} y={query.y}/>
<p><button onClick={setPosition}>Move</button></p>
</>
);
};
⬥ useRouteState(url, options?) has an optional second parameter in the shape of the navigation options. For example, we might want to pass { scroll: "off" } as options if we'd like to opt out from the default scroll-to-the-top behavior when the URL changes.
← Middleware | Type safety →