Skip to content

Instantly share code, notes, and snippets.

@staltz
Created April 7, 2015 06:43
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save staltz/0d92dc4409a471f83c48 to your computer and use it in GitHub Desktop.
Save staltz/0d92dc4409a471f83c48 to your computer and use it in GitHub Desktop.
Experiment with Cycle.js and React Native
'use strict';
var React = require('react-native');
var Cycle = require('cyclejs');
var {Rx, h} = Cycle;
var createExperimentalIOSRenderer = require('./src/ios-renderer.ios.js');
var {StyleSheet, Text, TextInput, View} = React;
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
}
});
var model = Cycle.createModel(() =>
({
number$: Rx.Observable.interval(1000).startWith(0).map(() => Math.ceil(Math.random()*1000))
})
);
var view = Cycle.createDataFlowNode(model =>
({
vtree$: model.get('number$').map(number =>
React.createElement(View, {style: styles.container},
React.createElement(Text, {style: styles.welcome}, String(number))
)
)
})
);
var renderer = createExperimentalIOSRenderer('react-native-sandbox');
renderer.inject(view).inject(model);
'use strict';
var Cycle = require('cyclejs');
var React = require('react-native');
var {AppRegistry, View} = React;
function createExperimentalIOSRenderer(target) {
return Cycle.createDataFlowSink((view) => {
AppRegistry.registerComponent(target, () => React.createClass({
componentWillMount: function() {
view.get('vtree$').subscribe((vtree) => this.setState({vtree: vtree}))
},
getInitialState: function() {
return {vtree: React.createElement(View)};
},
render: function() {
return this.state.vtree;
}
}));
});
}
module.exports = createExperimentalIOSRenderer;
@staltz
Copy link
Author

staltz commented Apr 7, 2015

Cycle.js version 0.18.2
React Native version 0.3.4

@jonrh
Copy link

jonrh commented May 27, 2015

Sorry for being slightly off-topic, couldn't find this in a search. Is it a convention to end a variable with $ to indicate it's an Observable?

@arlair
Copy link

arlair commented Jun 6, 2015

@jonrh Yes, it's an Observable convention and "required convention when working with custom elements". The start of the FAQ mentions this if you want to check it out 😄
https://github.com/staltz/cycle/blob/master/docs/faq.md

@jonrh
Copy link

jonrh commented Jun 8, 2015

@arlair Thanx! I just stumbled upon the FAQ and was going to update my comment, you beat me to it! It's weird that you don't get notified of replies to gists. Thanks again!

@rofrol
Copy link

rofrol commented Sep 19, 2015

@arlair dead link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment