Languages
[Edit]
EN

Preact - preact-router default routing when path is incorrect

6 points
Created by:
Mahir-Bright
1311

In this short article, we would like to show how to add default preact routing when there is not available any matching path.

Quick solution:

<Router>
    <Page1Component path="/page-1" />
    <Page2Component path="/page-2" />
    <Page3Component path="/page-3" />
    {/* ... */}
    <DefaultComponent default={true} />
</Router>

 

Practical example

In this section, you can find default routing that displays error component when desired path is not available.

import { h } from 'preact';
import { Router } from 'preact-router';

import Home from '../../routes/home';
import Users from '../../routes/users';
import Gallery from '../../routes/gallery';
import About from '../../routes/about';
import Error from '../../routes/error';

import style from './app.scss';

const App = () => {
	return (
		<div id="app" class={style.app}>
			<Router>
				<Home path="/" />
				<Users path="/users" />
				<Gallery path="/gallery" />
				<About path="/about" />
				<Error default={true} />
			</Router>
		</div>
	);
};

export default App;

Where: <Error /> component will be used when path is not matched to any above paths.

 

References

  1. Handling URLS with preact-router - GitHub
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join