Skip to content

Instantly share code, notes, and snippets.

@fakenickels
Created January 3, 2018 14:20
Show Gist options
  • Select an option

  • Save fakenickels/6d2f7ab0a312de7df29175f621872a43 to your computer and use it in GitHub Desktop.

Select an option

Save fakenickels/6d2f7ab0a312de7df29175f621872a43 to your computer and use it in GitHub Desktop.
/* 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