Skip to main content

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โ€‹

OptionTypeDescription
deploymentKeystring(Optional) Overrides the default deployment key.
installModeInstallModeDetermines how updates are applied. See below.
mandatoryInstallModeInstallModeHow to install mandatory updates (default: IMMEDIATE).
minimumBackgroundDurationnumberDelay background installs until app is in background for this many seconds.
updateDialogboolean | objectShows an update dialog (only for mandatory updates by default). Can be customized.
rollbackRetryOptionsobjectRetry 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}`);
}
);