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
Preact.Router.Make creates several functions that you can use, including:
- A hook
t optionthat 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 -> unitto push a route manually.
replace : t -> unitto replace a route manually.
Let me know if you try it out and have any comments!