CodePush Strategy for Beta Testing

Author

Spencer Carli

Developer, cat dad, and devout pizza lover. Teaching at React Native School and building apps with Handlebar Labs.

Last Updated: October 9, 2019

CodePush

CodePushManager.js

import React from 'react';
import { Platform } from 'react-native';
import codePush from 'react-native-code-push';
import AsyncStorage from '@react-native-community/async-storage';

const checkIfBetaTester = async () => {
  const res = await AsyncStorage.getItem('MyApp::IS_BETA_TESTER');
  return res === 'true';
};

const codepushKeys = {
  staging: Platform.select({
    ios: 'IOS_STAGING_KEY',
    android: 'ANDROID_STAGING_KEY',
  }),
  production: Platform.select({
    ios: 'IOS_PRODUCTION_KEY',
    android: 'ANDROID_PRODUCTION_KEY',
  }),
};

class CodePushManager extends React.Component {
  componentDidMount() {
    codePush
      .notifyAppReady()
      .then(() => checkIfBetaTester())
      .then((isBetaTester) => {
        if (isBetaTester) {
          codePush.sync({
            deploymentKey: codepushKeys.staging,
            installMode: codePush.InstallMode.IMMEDIATE,
            updateDialog: true,
          });
        } else {
          codePush.sync({ deploymentKey: codepushKeys.production });
        }
      });
  }

  render() {
    return null;
  }
}

export default codePush({ checkFrequency: codePush.CheckFrequency.MANUAL })(
  CodePushManager
);
React Native School Logo

React Native School

Want to further level up as a React Native developer? Join React Native School! You'll get access to all of our classes and our private Slack community.

Join the email list to be notified of all new lessons and classes!