From b8691be3aa456b3f3c5a9853285528885b506d73 Mon Sep 17 00:00:00 2001 From: Tigran Balayan Date: Thu, 22 Dec 2016 11:04:15 +0400 Subject: [PATCH 1/2] Fixes #1 Add RN 0.25+ support --- Experiment.js | 104 +++++++++++++++++++++++++------------------------- README.md | 12 +++--- Tracking.js | 6 +-- Variant.js | 35 +++++++++-------- index.js | 10 ++--- package.json | 2 +- 6 files changed, 89 insertions(+), 80 deletions(-) mode change 100644 => 100755 Experiment.js mode change 100644 => 100755 README.md mode change 100644 => 100755 Tracking.js mode change 100644 => 100755 Variant.js mode change 100644 => 100755 index.js mode change 100644 => 100755 package.json 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..8fc4bfe --- 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'; + +let { Experiment, Variant } = require('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)", From 65169d502dd7aa117e353f7c844b84a4a7a41834 Mon Sep 17 00:00:00 2001 From: Tigran Balayan Date: Thu, 22 Dec 2016 11:14:27 +0400 Subject: [PATCH 2/2] Fixes #1 Update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8fc4bfe..81c5c2d 100755 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ import { TouchableHighlight } from 'react-native'; -let { Experiment, Variant } = require('react-native-ab'); +import { Experiment, Variant } from 'react-native-ab'; let rnabtest = React.createClass({ render: function() {