Project Setup
This guide shows how to set up and use the CodePush.sync()
method in your React Native app.
๐ ๏ธ Basic Setupโ
import CodePush from "react-native-code-push"; // Old Architecture
import CodePush from "@code-push-next/react-native-code-push"; // New Architecture
useEffect(() => {
CodePush.sync({
installMode: CodePush.InstallMode.IMMEDIATE,
updateDialog: true,
});
}, []);
โ๏ธ CodePush.sync()
Options Explainedโ
CodePush.sync(options?, syncStatusChangeCallback?, downloadProgressCallback?);
๐ง Optionsโ
Option | Type | Description |
---|---|---|
deploymentKey | string | (Optional) Overrides the default deployment key. |
installMode | InstallMode | Determines how updates are applied. See below. |
mandatoryInstallMode | InstallMode | How to install mandatory updates (default: IMMEDIATE ). |
minimumBackgroundDuration | number | Delay background installs until app is in background for this many seconds. |
updateDialog | boolean | object | Shows an update dialog (only for mandatory updates by default). Can be customized. |
rollbackRetryOptions | object | Retry settings if update fails. |
๐ InstallMode
Optionsโ
IMMEDIATE
โ Applies the update immediately.ON_NEXT_RESTART
โ Applies the update on next app restart.ON_NEXT_RESUME
โ Applies when app resumes from the background.
๐ฌ updateDialog
Customizationโ
You can pass a custom object to show different messages:
updateDialog: {
appendReleaseDescription: true,
descriptionPrefix: '\n\nChange log:\n',
mandatoryContinueButtonLabel: 'Update now',
mandatoryUpdateMessage: 'An important update is available.',
optionalIgnoreButtonLabel: 'Later',
optionalInstallButtonLabel: 'Install',
optionalUpdateMessage: 'A new update is available. Install now?',
title: 'Update Available',
}
๐ syncStatusChangeCallback
โ
A callback that receives sync status updates:
CodePush.sync({}, (status) => {
switch (status) {
case CodePush.SyncStatus.CHECKING_FOR_UPDATE:
console.log("Checking for update.");
break;
case CodePush.SyncStatus.DOWNLOADING_PACKAGE:
console.log("Downloading package.");
break;
case CodePush.SyncStatus.INSTALLING_UPDATE:
console.log("Installing update.");
break;
case CodePush.SyncStatus.UP_TO_DATE:
console.log("App is up to date.");
break;
case CodePush.SyncStatus.UPDATE_INSTALLED:
console.log("Update installed.");
break;
}
});
๐ฆ downloadProgressCallback
โ
Monitor download progress:
CodePush.sync({}, null, (progress) => {
console.log(`${progress.receivedBytes} of ${progress.totalBytes} received`);
});
๐งช Example: Full Setupโ
CodePush.sync(
{
installMode: CodePush.InstallMode.IMMEDIATE,
updateDialog: {
title: "Update Available",
optionalUpdateMessage: "There is a new version. Do you want to update?",
optionalInstallButtonLabel: "Yes",
optionalIgnoreButtonLabel: "Later",
},
},
(status) => {
console.log("Sync status:", status);
},
(progress) => {
console.log(`Received ${progress.receivedBytes} of ${progress.totalBytes}`);
}
);