'Is there any way to find out if a react-native app is about to update to a new version?

Is there any way to find out that an app is about to update to a new version? I would like to call removeItem from Async Storage at that time.

Background: The current beta version (still not released) has a list which has a input field and a submit button. The first time the submit button is clicked, an alert box pops up letting the user know a couple of instructions about the workings of the application. To make this work, I have a showMessage variable in Async storage which becomes false once the message has been shown. The next version planned will have a new feature, so there are new instructions, thus I want to introduce a function that fires when the app updates. The function either remove the key value pair or make showMessage true. So when the app updates and the user clicks on the submit button for the first time, the alert box will popup with the instructions.



Solution 1:[1]

Just submit the next version with the new feature and the new instructions and a different key called showMessage2 which is set to true. have your app look for showMessage2 instead of showMessage. The user will get the new instructions when they get the new version, and not a moment sooner or later. The original showMessage key will be irrelevant but that's fine.

Solution 2:[2]

There is no function that will allow you to get notified 'if a react-native app is about to update to a new version'. Unless you're using something like CodePush, updates are handled outside of your code, and a new binary essentially just replaces your binary on the phone.

Instead, you could use a library like react-native-device-info and check the version of your app on launch or sometime before you get to your page using const version = DeviceInfo.getVersion(); You could then remove your AsyncStorage keys as appropriate for the version you detect.

Solution 3:[3]

Code Push

I am using code push, here is my code for triggering a code when the app updates.

import {MMKV} from 'react-native-mmkv';
import codePush from 'react-native-code-push';
import DeviceInfo from 'react-native-device-info';
export const storage = new MMKV({
  id: `version_storage`,
});

const VERSION_STORAGE_KEY = 'appVersion';

export const runThisFunctionOnAppStart = async () => {
  const version = `${DeviceInfo.getReadableVersion()}-`;
  const localPackage = await codePush.getUpdateMetadata();
  const appVersion=localPackage?version + localPackage.label:version + 'default'
   
  const previousVersion = storage.getString(VERSION_STORAGE_KEY);
  if (previousVersion) {
    if (previousVersion !== appVersion) {

     //this block of code will run when app will update

    }
  } else {
    storage.set(VERSION_STORAGE_KEY, appVersion);
  }
};

NOTE: you can run runThisFunctionOnAppStart in useEffect of App.js

Without Codepush

import {MMKV} from 'react-native-mmkv';
import DeviceInfo from 'react-native-device-info';
export const storage = new MMKV({
  id: `version_storage`,
});

const VERSION_STORAGE_KEY = 'appVersion';

export const runThisFunctionOnAppStart = async () => {
  const appVersion = DeviceInfo.getReadableVersion();
  const previousVersion = storage.getString(VERSION_STORAGE_KEY);
  if (previousVersion) {
    if (previousVersion !== appVersion) {

     //this block of code will run when app will update

    }
  } else {
    storage.set(VERSION_STORAGE_KEY, appVersion);
  }
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nunchucks
Solution 2 Jeremy
Solution 3 Muhammad Numan