Created
January 3, 2018 14:20
-
-
Save fakenickels/6d2f7ab0a312de7df29175f621872a43 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
| /* file: Recompose.re */ | |
| module type CreateWithStateParams = { | |
| /* This is the secret sauce ingredient, with it the users can pass whatever state they want */ | |
| type state; | |
| }; | |
| module CreateWithState = (Params: CreateWithStateParams) => { | |
| type value; | |
| type state = { value: Params.state }; | |
| type action = UpdateValue(Params.state); | |
| /* For stateful components in ReasonReact we need a reducerComponent */ | |
| let component = ReasonReact.reducerComponent("WithState"); | |
| /* Add defaultValue to props so the user can pass it dynamically */ | |
| let make = (~defaultValue: Params.state, children) => { | |
| ...component, | |
| initialState: () => { value: defaultValue }, | |
| reducer: (action, _state) => switch action { | |
| | UpdateValue(value) => ReasonReact.Update({value: value}) | |
| }, | |
| render: (self) => { | |
| let setState = self.reduce(value => UpdateValue(value)); | |
| children(~setState, ~state=self.state.value) | |
| } | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment