Created
January 12, 2016 17:48
-
-
Save carlesba/5bab2e81c2acf17d3182 to your computer and use it in GitHub Desktop.
Basic RxJS with Flux example
This file contains 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
// 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