forked from jaredpalmer/razzle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update reason example (jaredpalmer#513)
* Update reason example * Add url parser * Alias `ReasonReact.stringToElement` to `text`
- Loading branch information
1 parent
289e63a
commit 69c08e6
Showing
11 changed files
with
6,674 additions
and
425 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,5 +14,11 @@ | |
{ | ||
"dir": "src" | ||
} | ||
], | ||
"package-specs": [ | ||
{ | ||
"module": "es6-global", | ||
"in-source": false | ||
} | ||
] | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
let text = ReasonReact.stringToElement; | ||
|
||
type state = {count: int}; | ||
|
||
type action = | ||
| Increment | ||
| Decrement; | ||
|
||
let component = ReasonReact.reducerComponent("Counter"); | ||
|
||
/* underscore before names indicate unused variables. We name them for clarity */ | ||
let make = _children => { | ||
...component, | ||
initialState: () => {count: 0}, | ||
reducer: (action, state) => | ||
switch action { | ||
| Increment => ReasonReact.Update({count: state.count + 1}) | ||
| Decrement => ReasonReact.Update({count: state.count - 1}) | ||
}, | ||
render: self => { | ||
let message = "Count: " ++ string_of_int(self.state.count); | ||
<div className="App-intro"> | ||
(test(message)) | ||
<button onClick=(self.reduce(_event => Increment))> (test("+")) </button> | ||
<button onClick=(self.reduce(_event => Decrement))> (test("-")) </button> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
let text = ReasonReact.stringToElement; | ||
|
||
let component = ReasonReact.statelessComponent("Home"); | ||
|
||
/* underscore before names indicate unused variables. We name them for clarity */ | ||
let make = _children => { | ||
...component, | ||
render: _self => | ||
<p className="App-intro"> | ||
<code> (test("src/App.re")) </code> | ||
( | ||
test( | ||
". When you make edits, both the server and broswer will hot reload." | ||
) | ||
) | ||
</p> | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
let component = ReasonReact.statelessComponent("Link"); | ||
|
||
let make = (~href, ~className="", children) => { | ||
...component, | ||
render: self => | ||
ReasonReact.createDomElement( | ||
"a", | ||
~props={ | ||
"className": className, | ||
"href": href, | ||
"onClick": | ||
self.handle((event, _self) => { | ||
ReactEventRe.Mouse.preventDefault(event); | ||
ReasonReact.Router.push(href); | ||
}) | ||
}, | ||
children | ||
) | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
type action = | ||
| UpdateRoute(ReasonReact.Router.url); | ||
|
||
type state = ReasonReact.Router.url; | ||
|
||
/* copied from ReasonReact */ | ||
let urlToUrlList = url => | ||
switch url { | ||
| "" | ||
| "/" => [] | ||
| _ => | ||
/* remove the preceeding /, which every pathname seems to have */ | ||
let raw = Js.String.sliceToEnd(~from=1, url); | ||
/* remove the trailing /, which some pathnames might have. Ugh */ | ||
let raw = | ||
switch (Js.String.get(raw, Js.String.length(raw) - 1)) { | ||
| "/" => Js.String.slice(~from=0, ~to_=-1, raw) | ||
| _ => raw | ||
}; | ||
raw |> Js.String.split("/") |> Array.to_list; | ||
}; | ||
|
||
let component = ReasonReact.reducerComponent("Router"); | ||
|
||
let make: | ||
(~initialUrl: option(string), 'a) => ReasonReact.component(state, _, action) = | ||
(~initialUrl, children) => { | ||
...component, | ||
initialState: () => | ||
switch initialUrl { | ||
| Some(url) => {path: urlToUrlList(url), hash: "", search: ""} | ||
| None => ReasonReact.Router.dangerouslyGetInitialUrl() | ||
}, | ||
reducer: (action, _state) => | ||
switch action { | ||
| UpdateRoute(url) => ReasonReact.Update(url) | ||
}, | ||
subscriptions: ({send}) => [ | ||
Sub( | ||
() => ReasonReact.Router.watchUrl(url => send(UpdateRoute(url))), | ||
ReasonReact.Router.unwatchUrl | ||
) | ||
], | ||
render: ({state}) => children(state) | ||
}; |
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
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
Oops, something went wrong.