Routing
Routing in your components is possible via @webcomp/router
module.
WebComp router is a modern client side router that provides Express-like route handling, supports history API and hash mode and provides some higher order helpers. Let's go through everything.
Basic usage
Here's how you can define routes in your component:
Your route handler function will receive a url
object with the following structure:
params
- object with url paramspath
- Full path stringquery
- Parsed object from query string
Router Methods and Properties
Besides push
and on
that we've already seen, router provides a few additional methods and configuration options.
Router.mode
Router.mode
Default:
hash
A string indicating router's operation mode. Can be hash
or history
. To configure router for a different mode, assign a new value to it:
Keep in mind that this will configure the entire router, so you should only do it once at the top level of your app (before you register any of your components).
Router.root
Router.root
Default:
/
A string indicating router's root path. Same as with mode, you can assign a new value to set it:
This should also be configured once at the top level of your app.
`on(route, handlerFunction, persist)`
`on(route, handlerFunction, persist)`
Define a new handler for a route. Optional persist
argument will prevent a handler from flushing if Router.flushHandlers
is called.
Calling Router.on
will return a string ID which you can use to remove handlers later if needed. Usage:
removeHandler(id)
removeHandler(id)
Removes previously defined route handler by it's ID. Usage:
flushHandlers()
flushHandlers()
Removes all handlers from the router except ones that were defined with persist
flag.
dangerouslyFlushRouter()
dangerouslyFlushRouter()
Completely resets router to it's default state. Root is set to '/'
, mode to 'hash'
and all route handlers are removed (even persisted ones).
You will most likely never need to use this method. If you must, use with caution.
push(path)
Navigate to a new route. Uses `pushState` when in `history` mode. Usage:
replace(path)
replace(path)
Navigate to a new route and replace the history state instead of pushing.
Note: Only available in history
mode.
Routerize
decorator
Routerize
decoratorDefining routes explicitly isn't always convenient. Some times you just want to watch what happens and do stuff based on that. For these use cases, WebComp provides a @routerize
decorator:
Here's the structure of this.props.router
prop that @routerize
gives you:
This will be updated on every route change, so you can rely on the prop directly, without needing to specify route handlers.
Note: Due to how router works by default, your component will be re-rendered on mount when routerized. It's usually not a big deal, but if it is, you should use a custom router (see below) with a skipInitial
option.
<Route /> Component
If you only need to watch routes to change your rendered content, you can use <Route />
component to do that:
Shallow routes
Besides child node, Route
also supports passing a `component` prop for render. If you do this, rendered component will automatically be routerized. You can bypass this behavior by using shallow
prop.
In the above example, FancyHeader
component on route /
will get this.props.router
, but the one on /settings
route won't.
Advanced Usage
Custom Routers
In some cases you need to have different router configurations for different components. WebComp allows you to create custom routers for this.
Default Router
is an instance of a WCRouter
class, which you can reuse or extend.
Custom router options
skipInitial
- If this is set, router won't fire handlers on initial page load.mode
- Set default mode. Can be'hash'
or'history'
.root
- Set custom default root
Routerizing with custom router
The @routerize
decorator uses default router instance. If you want to routerize your component with your custom router, you can use routerizeWith
decorator instead:
Using custom routers with <Route />
<Route />
You can also use custom routers with <Route />
component by providing it as a customRouter
prop:
Last updated