Opinionated higher-order component for establishing ad-hoc stream-based unidirectional data flows.

functional-reactive-programming, react, recompose, xstream
npm install @akoenig/adhoc@2.0.0


adhoc Build Status

Opinionated higher-order component for establishing ad-hoc stream-based unidirectional data flows.



import React from "react";
import { compose, withHandlers } from "recompose";

import { withStreams } from "@akoenig/adhoc";

const SearchBoxComponent = ({query, placeholder, onChange}) => (

const SearchBox = compose(
    // Define your streams here ...
    // The name of the stream attribute will also be the name of the prop which holds the
    // current value of the stream. Please note that the following `fold` does not
    // distinguish between several action types for streamlining this example, but you
    // could implement fully-fledged Redux-like reducers here.
    query: (stream, ownProps) => stream.fold((state, action) => action.payload, ""),
    onChange: ({ dispatch }) => e => dispatch({type: "an-action-type", payload: e.target.value})

export { SearchBox };


For me, Redux is still the number one choice when it comes to selecting a global state container for scalable applications and I recommended it a lot to my customers. Many of the use-cases out there can be covered with this stack quite well. Even when this technology stack is still popular, I made an interesting observation in my last projects. Recently, I developed a GraphQL API and the corresponding client application with React + Apollo. Everything fitted nicely due to the declarative nature of GraphQL. Anyways, mapping queries and mutations to UI components is cool, but having UI state is of course still real. Therefore, what to do in this case? Sharing the Redux store with the Apollo Client? Well, you can do that. I had different goals in mind:

  • Lightweight solution without a lot of boilerplate
  • Reactive (❤️ RxJS & xstream)
  • Fast implementation iterations when you have to deal with local UI state

The result is this higher-order component which allows you to establish ad-hoc stream-based unidirectional data flows. Let's examine one typical use-case:

I want to have a search box which handles the user input by 1.) storing the current value, 2.) checking if the user entered at least three characters and 3.) there should not be any interaction in the last 500 ms before sending the query to the backend system.

Functional Reactive Programming is one of the best paradigms for handling scenarios like that. Achieving a solution for that use-case above can be express with xstream – a functional reactive stream library – as:

import debounce from "xstream/extra/debounce";
import fromEvent from "xstream/extra/fromEvent";

const stream = fromEvent(document.getElementById('search'), 'keyup')
  .map(e => e.currentTarget.value)
  .filter(value => value.length >= 3)
      next: (value) => console.log(`Received debounced search query: ${value}`)

Lovely, isn't it? adhoc helps you to do exactly that within your React components.



  streamCreators: {
      [streamName: string]: (stream: Stream, props: Object) => Stream

Each last value of the stream will be mapped and passed as prop to the component. The streamName defines the name of the prop. The HoC will create one dedicated stream for the respective component, but you can define as much stream creators as you want. Those stream creators can be seen as "substreams" of the "über-stream". The underlying stream foundation is xstream.

Additionally, your component will also receive a dispatch prop, which can be used to dispatch actions. Those actions will flow through your stream. The signature of this function awaits a Flux Standard Action: dispatch({type: String, payload: any})


MIT © André König