Flyd React
Minimal React bindings for Flyd.
Installation
npm install flyd-react --save
Requirements
Since this library depends on React Hooks API, React version should be higher than 16.8.
Tutorial
Stream Connection
connect
function connects a stream to a component state internally and returns current value of the state.
The connected state will be updated automatically when the stream emits a new value.
;;;;; // An example stream which updates every second.const everySecond = ;const stream = flyd; // A container which connects to the stream.const Container = { const childProps = count: ; return React;}; // A view which displays the count.const View = <p>count</p>; // Mount the container.;
Lifecycle Optimization
Skip Reconnection
connect
accepts "dependency values" as React's useEffect
does.
Passing appropriate deps might be effective when it is in a performance critical case.
For most streams, connecting once when a component is mounted is enough.
Passing []
as a second argument skips all the subsequent connection after mount.
; // Using `[]` as deps skips all the subsequent connection.const Container = { const childProps = count: ; return React;};
Selective Stream
In the following example, passing [streamKey]
as deps only reconnects when streamKey
changes.
Note that passing []
will cause problems in this case because given that deps, connect
skips reconnecting to a new stream even when a new streamKey
is set.
; const streams = one: flyd two: flyd; const Container = { const childProps = count: ; return React;};