Skip to content

Instantly share code, notes, and snippets.

@dangdennis
Created December 1, 2018 03:47
Show Gist options
  • Select an option

  • Save dangdennis/f656c40a623c810b9ec5b9e9edaacf7b to your computer and use it in GitHub Desktop.

Select an option

Save dangdennis/f656c40a623c810b9ec5b9e9edaacf7b to your computer and use it in GitHub Desktop.
/* Like proptypes, you define your state type */
type state = {counter: int};
/* Define a Redux-like action variant here */
type action =
| Count(int);
/* Define the reducer component */
let component = ReasonReact.reducerComponent("SomeStatefulComponent");
let make = _children => {
...component,
initialState: () => {counter: 0},
/* Define a reducer method that states action and state */
reducer: (action, state) =>
/* Do pattern matching of all defined actions
to Update (Reason's setState and action creator) */
switch (action) {
| Count(int) => ReasonReact.Update({counter: int + state.counter})
},
render: self =>
<div>
<p>
{ReasonReact.string("Count: " ++ string_of_int(self.state.counter))}
</p>
<button onClick={_event => self.send(Count(5))}>
{ReasonReact.string("+5")}
</button>
</div>,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment