Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@carlesba
Created January 12, 2016 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlesba/5bab2e81c2acf17d3182 to your computer and use it in GitHub Desktop.
Save carlesba/5bab2e81c2acf17d3182 to your computer and use it in GitHub Desktop.
Basic RxJS with Flux example
// Store
import AppDispatcher from './dispatcher/AppDispatcher'
const list$ = Rx.Subject.startWith([])
function addElement (action) {
const newElement = action.data
const currentValue = list$.getValue()
list$.onNext(currentValue.concat(newElement))
}
AppDispatcher.register((action) => {
switch (action.type) {
case 'addElement'
addElement(action)
}
})
return {
subscribe: list$.subscribe,
currentData: list$.getValue
}
// Component
import elementsStore from 'store/elementsStore'
const appComponent = React.createElement({
getInitialState () {
return {
list: elementsStore.currentData()
}
}
componentDidMount () {
this.streamObservable = elementsStore.subscribe((data) => {
this.setState({
list: data
})
})
}
componentWillUnmount () {
this.streamObservable.disposeStream()
}
render () {
return (<ListElements list={this.state.list}) />
}
})
const ListElements = ({list}) => {
return (
list.map((element) => <Element data={element} />)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment