Hi all!
I just released bs-preact v0.2.0 with a new type-safe Router.
There are two new examples:
-
Using the raw URL for routing: https://bs-preact.netlify.com/RouterRaw.html
-
Using the typesafe parser/builder: https://bs-preact.netlify.com/Router.html
Here’s what a complete router looks like:
module P = Preact
module Router = struct
type t =
| Home
| Posts
| Post of int
include P.Router.Make (struct
type nonrec t = t
let mode = P.Router.Hash
let parse =
P.Router.Parser.(
parse
(oneOf
[ root Home
; s "posts" |> map Posts
; s "posts" </> int |> map (fun id -> Post id)
]))
let build =
P.Router.Builder.(
function
| Home -> root
| Posts -> root </> "posts"
| Post id -> root </> "posts" </> int id)
end)
end
The functor Preact.Router.Make
creates several functions that you can use, including:
- A hook
use
which returnst option
that can be used like this:let[@hook] route = Router.use ()
. - A link function that generates an
<a>
vnode that pushes the route on click that can be used like this:Router.link (Router.Post 123) [P.class' "something"] [P.string "Go to Post #123"]
. -
push : t -> unit
to push a route manually. -
replace : t -> unit
to replace a route manually.
Let me know if you try it out and have any comments!