Skip to content

Instantly share code, notes, and snippets.

@kgoggin
Created Jan 15, 2018
Embed
What would you like to do?
Reason Routing
let getRouteComponent: ReasonReact.Router.url => ReasonReact.reactElement =
(url) =>
switch (MainRoutes.match(url)) {
| Home(_url) => <Page message="Home" />
| Admin(route) => <AdminRoot route />
| NotFound(_url) => <Page message="404" />
};
open ReasonReact.Router;
module type RouteType = {type t; let match: url => t;};
module AdminRoutes: RouteType = {
type t =
| Root(url)
| Transactions(option(string), url)
| Members(option(string), url)
| NotFound(url);
let match = (url) =>
switch url.path {
| [] => Root(url)
| ["transactions"] => Transactions(None, url)
| ["transactions", id] => Transactions(Some(id), url)
| ["members"] => Members(None, url)
| ["members", id] => Members(Some(id), url)
| _ => NotFound(url)
};
};
module MainRoutes: RouteType = {
type t =
| Home(url)
| Admin(AdminRoutes.t)
| NotFound(url);
let match = (url) =>
switch url.path {
| [] => Home(url)
| ["admin", ...rest] => Admin(AdminRoutes.match({...url, path: rest}))
| _ => NotFound(url)
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment