Created
February 23, 2018 20:51
-
-
Save schinns/c511f32aafe147cc23bc8eea3309cf77 to your computer and use it in GitHub Desktop.
Simple ReasonReact Router Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let component = ReasonReact.statelessComponent("App"); | |
let make = (_children) => { | |
...component, | |
render: (_self) => | |
<div className="app"> | |
<View.Nav></View.Nav> | |
<Router.WithRouter> | |
...( | |
(~currentRoute) => | |
switch currentRoute { | |
| Home => <View.Home /> | |
| About => <View.About /> | |
| _ => <View.NotFound /> | |
} | |
) | |
</Router.WithRouter> | |
</div> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type routes = | |
| Home | |
| About | |
| NotFound; | |
let stringToRoutes = | |
fun | |
| "Home" => Home | |
| "About" => About | |
| "NotFound" => NotFound; | |
let routeToString = | |
fun | |
| Home => "/public/" | |
| About => "/public/about" | |
| NotFound => "/public/404"; | |
let urlToRoute: ReasonReact.Router.url => routes = | |
url => | |
switch url.path { | |
| [] => Home | |
| ["about"] => About | |
| _ => NotFound | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module WithRouter = { | |
type state = {currentRoute: Config.routes}; | |
type action = | |
| ChangeUrl(Config.routes); | |
let component = ReasonReact.reducerComponent("WithRouter"); | |
let make: | |
((~currentRoute: Config.routes) => ReasonReact.reactElement) => | |
ReasonReact.component(state, _, action) = | |
children => { | |
...component, | |
initialState: () => { | |
currentRoute: | |
ReasonReact.Router.dangerouslyGetInitialUrl() |> Config.urlToRoute | |
}, | |
reducer: (action, _state) => | |
switch action { | |
| ChangeUrl(route) => ReasonReact.Update({currentRoute: route}) | |
}, | |
subscriptions: ({send}) => [ | |
Sub( | |
() => | |
ReasonReact.Router.watchUrl(url => | |
send(ChangeUrl(url |> Config.urlToRoute)) | |
), | |
ReasonReact.Router.unwatchUrl | |
) | |
], | |
render:({state}) => children(~currentRoute=state.currentRoute) | |
}; | |
}; | |
module Link = { | |
let component = ReasonReact.statelessComponent("Link"); | |
let make = (~text, _children) => { | |
...component, | |
render: self => { | |
let href = Config.routeToString(Config.stringToRoutes(text)); | |
<a href=(href) onClick=(self.handle((event, _self) => { | |
ReactEventRe.Mouse.preventDefault(event); | |
ReasonReact.Router.push(href); | |
}))> | |
(ReasonReact.stringToElement(text)) | |
</a> | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Nav = { | |
let component = ReasonReact.statelessComponent("Nav"); | |
let make = _children => { | |
...component, | |
render: _self => | |
<ul> | |
<li> | |
<Router.Link text="Home"/> | |
</li> | |
<li> | |
<Router.Link text="About"/> | |
</li> | |
</ul> | |
} | |
}; | |
module Home = { | |
let component = ReasonReact.statelessComponent("Home"); | |
let make = _children => { | |
...component, | |
render: _self => | |
<div> | |
(ReasonReact.stringToElement("Home")) | |
</div> | |
} | |
}; | |
module About = { | |
let component = ReasonReact.statelessComponent("About"); | |
let make = _children => { | |
...component, | |
render: _self => | |
<div> | |
(ReasonReact.stringToElement("About")) | |
</div> | |
} | |
}; | |
module NotFound = { | |
let component = ReasonReact.statelessComponent("NotFound"); | |
let make = _children => { | |
...component, | |
render: _self => | |
<div> | |
(ReasonReact.stringToElement("404 - Not Found")) | |
</div> | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment