React Native course · Module 12: Publishing the app

expo-updates - OTA Updates

1 min read ·
In this lesson5

Update your app without rebuilding!

Installation

npx expo install expo-updates

app.json configuration

{
  "expo": {
    "updates": {
      "fallbackToCacheTimeout": 0,
      "url": "https://u.expo.dev/xxx-xxx-xxx"
    },
    "runtimeVersion": {
      "policy": "sdkVersion"
    }
  }
}

Publishing updates

# Publish update to preview channel
eas update --branch preview --message "Fixed a bug"

# Publish to production
eas update --branch production --message "New feature"

Checking for updates in code

import * as Updates from 'expo-updates';

async function checkForUpdates() {
  try {
    const update = await Updates.checkForUpdateAsync();

    if (update.isAvailable) {
      await Updates.fetchUpdateAsync();

      Alert.alert(
        'Update available',
        'Restart the app to install?',
        [
          { text: 'Later', style: 'cancel' },
          { text: 'Restart', onPress: () => Updates.reloadAsync() },
        ]
      );
    }
  } catch (error) {
    console.log('Error checking for updates:', error);
  }
}

Automatic updates

// In App.tsx
useEffect(() => {
  if (!__DEV__) {
    Updates.checkForUpdateAsync().then((update) => {
      if (update.isAvailable) {
        Updates.fetchUpdateAsync().then(() => {
          Updates.reloadAsync();
        });
      }
    });
  }
}, []);
Code for this lesson: App.tsx
import React, { useEffect } from 'react';
import { View, Text, Alert, StyleSheet } from 'react-native';
import * as Updates from 'expo-updates';

// Function that checks for OTA update availability and installs it
async function checkForUpdates() {
  try {
    // TODO: fetch the update info:
    //   const update = await Updates.checkForUpdateAsync();

    // TODO: if update.isAvailable === true:
    //   1. fetch the update:    await Updates.fetchUpdateAsync();
    //   2. reload the app:      await Updates.reloadAsync();

  } catch (error) {
    console.log('Error checking for updates:', error);
  }
}

export default function App() {
  useEffect(() => {
    // TODO: in production mode (!__DEV__) call checkForUpdates()

  }, []);

  return (
    <View style={styles.container}>
      <Text style={styles.title}>Atlantis</Text>
      <Text style={styles.subtitle}>Checking for updates...</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#1a5276',
    gap: 8,
  },
  title: {
    fontSize: 24,
    color: '#fff',
    fontWeight: '600',
  },
  subtitle: {
    fontSize: 16,
    color: '#aed6f1',
  },
});

Docs entries used in this lesson

Spotted a mistake in this lesson?

Check yourself

Answer the questions from this lesson. Pick an answer to see right away whether it is correct.

  1. 1. What do OTA Updates allow you to do?

  2. 2. How do you check for update availability in code?

These are 2 of 4 questions for this lesson. Solve the rest in the game.

Hands-on tasks in the game

  • Click in order

    Click the elements to publish an update:

  • Code editor

    Complete the code with expo-updates.

  1. QuestionOTA Updates
  2. Click in ordereas update command
  3. QuestioncheckForUpdateAsync
  4. QuestionfetchUpdateAsync
  5. QuestionreloadAsync
  6. Coding taskCheck for updates

Useful articles