[ANN]: bs-preact v0.2.0 is out, now with a type-safe Router!


#1

Hi all!

I just released bs-preact v0.2.0 with a new type-safe Router.

There are two new examples:

  1. Using the raw URL for routing: https://bs-preact.netlify.com/RouterRaw.html

  2. 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:

  1. A hook use which returns t option that can be used like this: let[@hook] route = Router.use ().
  2. 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"].
  3. push : t -> unit to push a route manually.
  4. replace : t -> unit to replace a route manually.

Let me know if you try it out and have any comments!