如何在我的 React Native Android 应用程序中显示当前的 CodePush 版本标签?

How do i show the current CodePush Version label in my React Native Android app?

寻找类似的东西: <Text>VERSION={CodePush.VersionLabel}</Text>

其中 CodePush.VersionLabel 类似于 "v6" 显示在 code-push deployment ls <MyApp>

我想在我的登录屏幕底部显示它。

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>
    );
}

注意:.label 属性 是CodePush使用的内部版本号(例如v24

componentDidMount() {
 CodePush.getCurrentPackage().then((update)=> {
    console.log('####### CodePush', update);
 });
}

如果没有可用更新,getUpdateMetadata() returns null...

解决方法:

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 };

示例输出:v1.4.4"v1.4.4 rev.5" 取决于状态。

这是一个如何将其用作自定义挂钩的示例。

1.准备挂钩

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。在功能组件中使用钩子

import { useOTAVersion } from 'your/hooks'

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

  return <Text>{appVersion}</Text>
}