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

React native 如何在我的React原生Android应用程序中显示当前的CodePush版本标签?,react-native,code-push,React Native,Code Push,寻找类似于: 版本={CodePush.VersionLabel} 其中CodePush.VersionLabel类似于“v6”,显示在代码推送部署ls 我想在我的登录屏幕底部显示此信息。componentDidMount(){ componentDidMount(){ codePush.getUpdateMetadata().then((metadata) =>{ this.setState({label: metadata.label, version: met

寻找类似于:

版本={CodePush.VersionLabel}

其中CodePush.VersionLabel类似于“v6”,显示在
代码推送部署ls

我想在我的登录屏幕底部显示此信息。

componentDidMount(){
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>
    );
}
codePush.getUpdateMetadata().then((元数据)=>{ this.setState({label:metadata.label,版本:metadata.appVersion,description:metadata.description}); }); } render(){ 返回( {this.state.version}.{this.state.label} ); }

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

如果没有可用的更新,getUpdateMetadata()将返回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>
}
从'your/hooks'导入{useOTAVersion}
常量CmpExample=()=>{
const{appVersion}=useOTAVersion()
返回{appVersion}
}

元数据
如果是使用二进制@julestroung打包的codepush捆绑包,则它将为
null
,因为包信息是通过异步IO API从某种内部codepush文件读取的
getCurrentPackage
现在是
import { useOTAVersion } from 'your/hooks'

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

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