Skip to content

Instantly share code, notes, and snippets.

@mjs2600
Created June 8, 2018 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjs2600/599a99254ce86cb42248133c16e9a978 to your computer and use it in GitHub Desktop.
Save mjs2600/599a99254ce86cb42248133c16e9a978 to your computer and use it in GitHub Desktop.
ReasonML Counter
type state = {count: int};
type action =
| Increment
| Decrement;
let component = ReasonReact.reducerComponent("State");
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 =>
<div>
<h1>
(ReasonReact.string("Count: " ++ string_of_int(self.state.count)))
</h1>
<button onClick=(_event => self.send(Increment))>
(ReasonReact.string("+"))
</button>
<button onClick=(_event => self.send(Decrement))>
(ReasonReact.string("-"))
</button>
</div>,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment