19

Looking for something like: <Text>VERSION={CodePush.VersionLabel}</Text>

Where CodePush.VersionLabel is something like "v6" that is displayed in code-push deployment ls <MyApp>

I'd like to show this at the bottom my login screen.

Doug
  • 14,387
  • 17
  • 74
  • 104

4 Answers4

31
componentDidMount(){
    codePush.getUpdateMetadata().then((metadata) =>{
      this.setState({label: metadata.label, version: metadata.appVersion, description: metadata.description});
    });
}

render() {
    return(
        <Text>{this.state.version}.{this.state.label}</Text>
    );
}

Note: The .label property is the internal build number used by CodePush (e.g. v24)

iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
Doug
  • 14,387
  • 17
  • 74
  • 104
  • 6
    `metadata` will be `null` if it is the codepush bundle packaged with the binary https://github.com/Microsoft/react-native-code-push/blob/master/docs/api-js.md#codepushgetupdatemetadata – arush_try.com Jun 07 '18 at 21:11
  • 1
    @julestruong because package info is readed from some kind of internal codepush file via async IO API – grexlort May 29 '19 at 13:35
8

If there are no updates available, getUpdateMetadata() returns null...

Workaround:

import codePush from "react-native-code-push";

async function getAppVersion() {
    const [{ appVersion }, update] = await Promise.all([
    codePush.getConfiguration(),
    codePush.getUpdateMetadata()
    ]);

    if (!update) {
        return `v${appVersion}`;
    }

    const label = update.label.substring(1);
    return `v${appVersion} rev.${label}`;
};

export { getAppVersion };

Example output: v1.4.4" OR v1.4.4 rev.5" depending on state.

Greg R Taylor
  • 3,470
  • 1
  • 25
  • 19
5

Here is an example of how to use it as a custom hook.

1. Prepare the hook

import { useEffect, useState } from 'react'
import codePush from 'react-native-code-push'
import DeviceInfo from 'react-native-device-info'

async function getOTAVersion() {
  try {
    const update = await codePush.getUpdateMetadata()

    return update ? update.label : null
  } catch (error) {
    return null
  }
}

export function useOTAVersion() {
  const [appVersion, setAppVersion] = useState(DeviceInfo.getReadableVersion())

  useEffect(() => {
    getOTAVersion().then((OTAVersion) => {
      if (OTAVersion) {
        setAppVersion(`${appVersion}/${OTAVersion}`)
      }
    })
  }, [])

  return { appVersion }
}

2. Use the hook inside functional components

import { useOTAVersion } from 'your/hooks'

const CmpExample = () => {
  const { appVersion } = useOTAVersion()

  return <Text>{appVersion}</Text>
}
4
componentDidMount() {
 CodePush.getCurrentPackage().then((update)=> {
    console.log('####### CodePush', update);
 });
}
Giang
  • 2,384
  • 2
  • 25
  • 26
  • `getCurrentPackage` is now [deprecated in favor of `getUpdateMetadata`](https://learn.microsoft.com/en-us/appcenter/distribution/codepush/react-native#api-reference) – Daniel Schlaug Jul 22 '19 at 07:27