Skip to content

Instantly share code, notes, and snippets.

@ide
Created December 2, 2015 21:37
Show Gist options
  • Save ide/9141ad48b76f2c85ce43 to your computer and use it in GitHub Desktop.
Save ide/9141ad48b76f2c85ce43 to your computer and use it in GitHub Desktop.
Decorators with Babel 6 and React Native
/**
* Sample React Native App
* https://github.com/facebook/react-native
*/
'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
TouchableOpacity,
View,
} = React;
import { createStore } from 'redux';
import { Provider, connect } from 'react-redux/native';
var autobind = require('autobind-decorator');
@connect(state => ({ count: state }))
class Babel extends React.Component {
render() {
let { count } = this.props;
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Redux + decorators + Babel 6 + React Native
</Text>
<TouchableOpacity onPress={this._handlePress}>
<Text style={styles.instructions}>
Press me
</Text>
</TouchableOpacity>
<Text style={styles.instructions}>
Pressed {count} time{count !== 1 ? 's' : ''}
</Text>
</View>
);
}
@autobind
_handlePress() {
this.props.dispatch({ type: 'INCREMENT' });
}
}
class App extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
store: createStore(counter),
};
}
render() {
return (
<Provider store={this.state.store}>
{() => <Babel />}
</Provider>
);
}
}
function counter(state = 0, action) {
switch (action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
}
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
AppRegistry.registerComponent('Babel', () => App);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment