Last active
February 25, 2016 12:23
-
-
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.
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
/* @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; | |
} | |
} |
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
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