Created
October 29, 2018 09:04
-
-
Save reaktivo/df30d8ba66e5912d96fb3a23c0649c4f to your computer and use it in GitHub Desktop.
Playing around with React Hooks + Callbags
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
import React, { useState, useEffect } from "react"; | |
import { interval, map, pipe, merge } from "callbag-basics"; | |
import subscribe from "callbag-subscribe"; | |
import of from "callbag-of"; | |
const usePipeStream = fns => { | |
const [value, setValue] = useState(null); | |
// maybe should use useCallback on the pipe and then run it | |
useEffect( | |
() => | |
pipe( | |
...fns, | |
subscribe(setValue) | |
), | |
fns | |
); | |
return value; | |
}; | |
const intervalImmediate = intervalMs => { | |
let count = 0; | |
return pipe( | |
merge(of(0), interval(intervalMs)), | |
map(() => count++) | |
); | |
}; | |
const ticker = intervalImmediate(1000); | |
// const ticker = interval(1000); | |
const mapper = map(x => `${x} count`); | |
export default function Timer() { | |
const count = usePipeStream([ticker, mapper]); | |
console.log({ count }); | |
return <h1>{count}</h1>; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment