Created
December 1, 2018 03:47
-
-
Save dangdennis/f656c40a623c810b9ec5b9e9edaacf7b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /* 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