Skip to content

Instantly share code, notes, and snippets.

@justinwoo
Last active February 25, 2016 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save justinwoo/7cc1cc5d8e06e3aa072d to your computer and use it in GitHub Desktop.
Save justinwoo/7cc1cc5d8e06e3aa072d to your computer and use it in GitHub Desktop.
A React driver for Cycle.js that handles injection via context. Warning: you should probably not actually use this and prefer making your own driver with props instead.
/* @flow */
import Rx from 'rx';
import React from 'react';
import ReactDOM from 'react-dom';
export function makeReactDriver<T>(
containerId: string,
childContextTypes: any,
getChildContext: () => {[key: string]: () => void},
source: T
): (elements$: Rx.Observable<ReactElement>) => T {
const container = document.getElementById(containerId);
const ContextProvider = React.createClass({
childContextTypes,
getChildContext,
render() {
return this.props.children;
}
});
return function reactDriver(element$: Rx.Observable<ReactElement>) {
element$.subscribe(
(element) =>
ReactDOM.render(
<ContextProvider>
{element}
</ContextProvider>,
container
),
(err) => console.error('Error occured while rendering React', err)
);
return source;
}
}
const boardClear$ = new Rx.Subject();
const childContextTypes = {
// using any unless someone tells me how to use Flowtype with this.
boardClear$: React.PropTypes.any
};
function getChildContext(): any {
return {
boardClear$
};
};
const source = {
boardClear$
};
let drivers = {
react: makeReactDriver('app', childContextTypes, getChildContext, source)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment