-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimple.re
More file actions
63 lines (58 loc) · 1.83 KB
/
Simple.re
File metadata and controls
63 lines (58 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
type state = {counter: int};
type action =
| Double(int)
| Increment
| Decrement;
let serializeState = state =>
/* We expose a really simple URL creation method - feel free to generate your string URL in any way */
Oolong.Url.make([string_of_int(state.counter)], "", "");
let program = Oolong.routerProgram(~serializeState, "CounterExample");
let counterProgram = {
...program,
init: (path, _search, _hash) =>
switch (path) {
| [counter] => Oolong.State({counter: int_of_string(counter)})
| _ => Oolong.State({counter: 0})
},
fromRoute: (routeAction, _state) =>
switch (routeAction) {
| Push([counter], _search, _hash)
| Replace([counter], _search, _hash)
| Pop([counter], _search, _hash) =>
Oolong.State({counter: int_of_string(counter)})
| _ => Oolong.State({counter: 0})
},
toRoute: (action, state) =>
switch (action) {
| Double(num) =>
/* This is using Replace just as an example - Push might be more appropriate */
Oolong.Replace({counter: num * 2})
| Increment => Oolong.Push({counter: state.counter + 1})
| Decrement => Oolong.Push({counter: state.counter - 1})
},
render: self =>
<div>
{ReasonReact.string(string_of_int(self.state.counter))}
<button onClick={_ => self.send(Increment)}>
{ReasonReact.string("Increment")}
</button>
<button onClick={_ => self.send(Decrement)}>
{ReasonReact.string("Decrement")}
</button>
<button onClick={_ => self.send(Double(self.state.counter))}>
{ReasonReact.string("Double")}
</button>
</div>,
};
[%raw
{|
function() {
var app = document.createElement("div");
app.id = "app";
document.body.appendChild(app);
}()
|}
];
Oolong.run(~router=Oolong.Router.hash(), counterProgram, view =>
ReactDOMRe.renderToElementWithId(view, "app")
);