Specify your routes with paths and corresponding components
const routes = [
{
path: '/',
component: MainPage
},
...
]
Import
RouterView
whereever you want to render it and use it like you use a normal component
import { RouterView } from './router.example';
const App = props => (
<div>
<RouterView />
</div>
);
Because RouterView is just a reference to your real component, you can pass along props like you normally would
<RouterView themeColor={state.themeColor} />
Call
router.push
with one of your specified paths to update the view
import router from 'router.example';
<button onclick={() => router.push('/subpage')}> Navigate! </button>
-
Your app needs to have an action called
updateRouterView
that changes the state everytime it's called.
The router module needs access to the actions so it's able to callupdateRouterView
.
By changing the state we force hyperapp to update the view once RouterView is changed.
It's a bit hacky, but it works :)
Seemain.example.js
for an example. -
Run
router.init()
immediately after callingapp(state, actions, Component, target)
to start hyperapp.
This matches the current URL to update the view and sets up a listener for clicks on the browser's native forwards and backwards buttons. -
In order for the router to work, your server has to respond with your
index.html
no matter which URL is requested.
If you can configure your server with a.htaccess
file, you can simple put a.htaccess
containingFallbackResource /index.html
in your root directory.
For webpack's dev server, adddevServer: { historyApiFallback: true }
to your webpack config.
This is just a really simple routing implementation, allowing you to
associate different pages/components with URL paths to build a very basic Single Page Application.
It does not handle query strings, aliases, dynamic route matching and all the other features advanced routers offer.