diff --git a/Experiment.js b/Experiment.js
old mode 100644
new mode 100755
index 27e3eca..7cf3abb
--- a/Experiment.js
+++ b/Experiment.js
@@ -1,46 +1,20 @@
-var React = require('react-native');
-var {
+import React, {
+ Component,
+ PropTypes
+} from 'react';
+
+import {
AsyncStorage,
- PropTypes,
View
-} = React;
-
-var Experiment = React.createClass({
-
- propTypes: {
- name: PropTypes.string.isRequired,
- children: ((props, propName) => {
- var children = props[propName];
- if (!Array.isArray(children) || children.length < 2) {
- return new Error('You must have at least 2 Variants.');
- }
- for (child of children) {
- if (!child.type.prototype.isVariant) {
- return new Error('One or more children is not a Variant.');
- }
- }
- }),
- choose: PropTypes.func,
- onChoice: PropTypes.func,
- onRawChoice: PropTypes.func
- },
+} from 'react-native';
- getDefaultProps() {
- return {
- choose(variants) {
- var choice = Math.floor(Math.random() * variants.length);
- return variants[choice];
- },
- onChoice(testName, variantName) { /* noop */ },
- onRawChoice(test, variant) { /* noop */ }
- }
- },
-
- getInitialState() {
- return {
+class Experiment extends Component {
+ constructor(props) {
+ super(props);
+ this.state = {
variant:
- }
- },
+ };
+ }
componentWillMount() {
this.variants = this.props.children;
@@ -48,7 +22,7 @@ var Experiment = React.createClass({
this.key = 'react-native-ab:Experiment:' + this.props.name;
AsyncStorage.getItem(this.key, ((err, variantName) => {
- var choice;
+ let choice;
if (err || !variantName) {
choice = this.props.choose(this.variants);
AsyncStorage.setItem(this.key, choice.props.name); // Optimistic update
@@ -62,32 +36,60 @@ var Experiment = React.createClass({
variant: choice
});
}).bind(this));
- },
-
- render() {
- return this.state.variant;
- },
+ }
getActiveVariant() {
return this.state.variant;
- },
+ }
getName() {
return this.props.name;
- },
+ }
getVariant(name) {
return this.variants.find((v) => v.props.name == name);
- },
+ }
reset(cb) {
AsyncStorage.removeItem(this.key, cb);
- },
+ }
_onChange(changed) {
var newState = Object.assign({}, this.state, changed);
this.setState(newState);
}
-});
-module.exports = Experiment;
+ render() {
+ return this.state.variant;
+ }
+
+};
+
+Experiment.propTypes = {
+ name: PropTypes.string.isRequired,
+ children: ((props, propName) => {
+ let children = props[propName];
+ if (!Array.isArray(children) || children.length < 2) {
+ return new Error('You must have at least 2 Variants.');
+ }
+ for (child of children) {
+ if (!child.type.prototype.isVariant) {
+ return new Error('One or more children is not a Variant.');
+ }
+ }
+ }),
+ choose: PropTypes.func,
+ onChoice: PropTypes.func,
+ onRawChoice: PropTypes.func
+};
+
+Experiment.defaultProps = {
+ choose(variants) {
+ let choice = Math.floor(Math.random() * variants.length);
+ return variants[choice];
+ },
+ onChoice(testName, variantName) { /* noop */ },
+ onRawChoice(test, variant) { /* noop */ }
+};
+
+export default Experiment;
diff --git a/README.md b/README.md
old mode 100644
new mode 100755
index 5aaa5d4..81c5c2d
--- a/README.md
+++ b/README.md
@@ -18,17 +18,19 @@ All you need is to `require` the `react-native-ab` module and then use the provi
```javascript
-var React = require('react-native');
-var {
+import React from 'react';
+
+import {
AppRegistry,
StyleSheet,
Text,
View,
TouchableHighlight
-} = React;
-var { Experiment, Variant } = require('react-native-ab');
+} from 'react-native';
+
+import { Experiment, Variant } from 'react-native-ab';
-var rnabtest = React.createClass({
+let rnabtest = React.createClass({
render: function() {
return (
diff --git a/Tracking.js b/Tracking.js
old mode 100644
new mode 100755
index 28b952b..bfb471a
--- a/Tracking.js
+++ b/Tracking.js
@@ -1,6 +1,6 @@
-var React = require('react-native');
-var AdSupportIOS = require('AdSupportIOS');
-var Dimensions = require('Dimensions');
+import React from 'react';
+import AdSupportIOS from 'AdSupportIOS';
+import { Dimensions } from 'react-native';
module.exports = {
getAdvertisingId(cb) {
diff --git a/Variant.js b/Variant.js
old mode 100644
new mode 100755
index 410ecac..3322a2c
--- a/Variant.js
+++ b/Variant.js
@@ -1,21 +1,26 @@
-var React = require('react-native');
-var { PropTypes } = React;
+import React, {
+ Component,
+ PropTypes
+} from 'react';
-var Variant = React.createClass({
- propTypes: {
- name: PropTypes.string.isRequired,
- children: PropTypes.element.isRequired
- },
-
- render() {
- return this.props.children;
- },
+class Variant extends Component {
+ constructor(props) {
+ super(props);
+ this.isVariant = true;
+ }
getName() {
return this.props.name;
- },
+ }
+
+ render() {
+ return this.props.children;
+ }
+};
- isVariant: true
-});
+Variant.propTypes = {
+ name: PropTypes.string.isRequired,
+ children: PropTypes.element.isRequired
+};
-module.exports = Variant;
+export default Variant;
diff --git a/index.js b/index.js
old mode 100644
new mode 100755
index 24f8662..9cf72e7
--- a/index.js
+++ b/index.js
@@ -1,5 +1,5 @@
-module.exports = {
- Experiment: require('./Experiment'),
- Tracking: require('./Tracking'),
- Variant: require('./Variant')
-};
+import Experiment from './Experiment';
+import Tracking from './Tracking';
+import Variant from './Variant';
+
+export { Experiment, Tracking, Variant };
\ No newline at end of file
diff --git a/package.json b/package.json
old mode 100644
new mode 100755
index 15f3725..2078a2c
--- a/package.json
+++ b/package.json
@@ -4,7 +4,7 @@
"type": "git",
"url": "https://github.com/lwansbrough/react-native-ab.git"
},
- "version": "0.1.2",
+ "version": "0.1.3",
"description": "A component for rendering A/B tests in React Native",
"main": "index.js",
"author": "Lochlan Wansbrough (http://lwansbrough.com)",