Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 53 additions & 51 deletions Experiment.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,54 +1,28 @@
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: <View/>
}
},
};
}

componentWillMount() {
this.variants = this.props.children;

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
Expand All @@ -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;
12 changes: 7 additions & 5 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<View style={styles.container}>
Expand Down
6 changes: 3 additions & 3 deletions Tracking.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
35 changes: 20 additions & 15 deletions Variant.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 5 additions & 5 deletions index.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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 };
2 changes: 1 addition & 1 deletion package.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]> (http://lwansbrough.com)",
Expand Down