Skip to content

Instantly share code, notes, and snippets.

@hanford
Last active October 3, 2018 16:53
Show Gist options
  • Save hanford/05905d85cd571559268cd4c19e3feb80 to your computer and use it in GitHub Desktop.
Save hanford/05905d85cd571559268cd4c19e3feb80 to your computer and use it in GitHub Desktop.
type state = {
count: int,
};
type action =
| Add
| Subtract;
module Counter = {
let component = ReasonReact.reducerComponent("Counter")
let make = _children => {
...component,
initialState: () => { count: 0 },
reducer: (action, state) => {
switch (action) {
| Add => ReasonReact.Update({ count: state.count + 1 })
| Subtract => ReasonReact.Update({ count: state.count - 1 })
}
},
render: self => {
let message = "Count #" ++ string_of_int(self.state.count);
<div>
<div>(message |> ReasonReact.string)</div>
<button onClick=(_event => Add |> self.send)>(ReasonReact.string("Add"))</button>
<button onClick=(_event => Subtract |> self.send)>(ReasonReact.string("Subtract"))</button>
</div>
}
};
};
ReactDOMRe.renderToElementWithId(<Counter />, "preview");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment