Skip to content

Instantly share code, notes, and snippets.

@Denommus
Last active October 6, 2017 00:06
Show Gist options
  • Save Denommus/f9c7b6aabbb262ec92f8c75237f52ae9 to your computer and use it in GitHub Desktop.
Save Denommus/f9c7b6aabbb262ec92f8c75237f52ae9 to your computer and use it in GitHub Desktop.
Integrating ReasonReact with Erratique's React
open ReactErratique;
let initial = ref 0;
let (timeS, timeC) = S.create !initial;
Js.log timeS;
let timeIncrement () => {
initial := !initial + 1;
timeC !initial
};
let timerId = Js.Global.setInterval timeIncrement 1000;
let vdomS =
S.map
(
fun time => {
let timeMessage = time == 1 ? "second" : "seconds";
let greeting = {j|You've spent $time $timeMessage on this page!|j};
<div> (ReasonReact.stringToElement greeting) </div>
}
)
timeS;
let component = ReactReact.componentFromSignal "CounterNew" vdomS;
let make _children => component;
open ReactErratique;
type action 'x =
| Tick 'x;
type state = {
vdom: ReasonReact.reactElement,
subscriberId: ref (signal unit)
};
let componentFromSignal name vdomS => {
let component = ReasonReact.reducerComponent name;
{
...component,
initialState: fun () => {
vdom: <div> (ReasonReact.stringToElement "Not mounted yet") </div>,
subscriberId: ref (fst (S.create ()))
},
reducer: fun action state =>
switch action {
| Tick x => ReasonReact.Update {...state, vdom: x}
},
didMount: fun {state, reduce} => {
state.subscriberId :=
S.map
(
fun x => {
Js.log "foo";
reduce (fun _event => Tick x) ()
}
)
vdomS;
ReasonReact.NoUpdate
},
render: fun {state: {vdom, subscriberId}} => {
Js.log subscriberId;
vdom
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment