121
20%
MIT
Tiny wrapper around ReasonReact.Router

reroute

a fast, declarative microrouter for reason-react

Getting started

Installation

Open a Terminal in your project's folder and run,

$ yarn add reason-reroute

After installation, you will need to add this library to your bsconfig.json dependencies

"bs-dependencies": [
  "reason-react",
  "reason-reroute"
],

Usage

  • RE
  • ML
module RouterConfig = {
  type route =
    | Admin
    | Home;
  let routeFromUrl = (url: ReasonReact.Router.url) =>
    switch url.path {
    | ["admin"] => Admin
    | [] => Home
    };
  let routeToUrl = (route: route) =>
    switch route {
    | Admin => "/admin"
    | Home => "/"
    };
};

module Router = ReRoute.CreateRouter(RouterConfig);

let component = ReasonReact.statelessComponent("App");

let make = _children => {
  ...component,
  render: _self =>
    <Router.Container>
      ...(
           (~currentRoute) =>
             switch currentRoute {
             | RouterConfig.Admin => <Admin />
             | RouterConfig.Home => <Home />
             }
         )
    </Router.Container>
};
module RouterConfig =
  struct
    type route =
      | Admin
      | Home
    let routeFromUrl (url : ReasonReact.Router.url) =
      match url.path with | "admin"::[] -> Admin | [] -> Home
    let routeToUrl (route : route) =
      match route with | Admin  -> "/admin" | Home  -> "/"
  end
module Router = ReRoute.CreateRouter(RouterConfig)
let component = ReasonReact.statelessComponent "App"
let make _children =
  {
    component with
    render =
      (fun _self  ->
         ((Router.Container.createElement
             ~children:(fun ~currentRoute  ->
                          match currentRoute with
                          | RouterConfig.Admin  ->
                              ((Admin.createElement ~children:[] ())[@JSX ])
                          | RouterConfig.Home  ->
                              ((Home.createElement ~children:[] ())[@JSX ]))
             ())[@JSX ]))
  }

API

Sections below are under construction.

Container

Rationale

Credits

The concept of reroute has been highly influenced by @thangngoc89 and his reference implementation. Thank you for pushing this forward!

License

MIT (c) 2018 Callstack