How to use the wouter-preact.useRouter function in wouter-preact

To help you get started, we’ve selected a few wouter-preact examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kidqueb / preact-polka-ssr / src / client / components / NestedRouter.js View on Github external
export default ({ req, base, children }) => {
	const hook = getRouterHook(req); // Determine if it's SSR or browser
	const router = useRouter({ hook, base }); // Set reference to our router
	const [location] = useLocation(); // Set reference of the url

	// If the current url doesn't start with our `base` then there's no way
	// that a nested route would be match so we can just return null.
	return location.startsWith(base) 
		? toChildArray(children).map(child => {
				const [matches, params] = router.matcher(base + child.props.path, location);

				return matches 
					? h(child.type, { ...params, ...child.props })
					: null
			})
	  : null;
};

wouter-preact

Minimalist-friendly ~1.5KB router for Preact

Unlicense
Latest version published 23 days ago

Package Health Score

80 / 100
Full package analysis