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.
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.
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
)
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.
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>
}
componentDidMount() {
CodePush.getCurrentPackage().then((update)=> {
console.log('####### CodePush', update);
});
}