Skip to main content

Javascript Reference (API)

The CodePush plugin has two components:

  1. JavaScript module - Interact with the service at runtime (check updates, inspect metadata)
  2. Native API (Objective-C/Java) - Bootstrap the app with the correct JS bundle location

Top-level Methods

When you import react-native-code-push, you get these methods:

MethodDescription
allowRestartRe-allows programmatic restarts after disallowRestart
checkForUpdateCheck if an update is available
disallowRestartTemporarily block restarts during critical flows
getUpdateMetadataGet metadata about installed updates
notifyAppReadyMark update as successful (prevents rollback)
restartAppImmediately restart the app
syncCheck, download, and install updates in one call
clearUpdatesClear all downloaded updates.
Note: we don’t recommend using this method in scenarios other than OTA debugging/testing; CodePush will call it automatically when appropriate. Using it in other cases could lead to unpredictable behavior.

codePush

// Wrapper function
codePush(rootComponent: React.Component): React.Component;
codePush(options: CodePushOptions)(rootComponent: React.Component): React.Component;

Wraps a React component to automatically sync updates on mount. Internally calls sync in componentDidMount.

Usage Examples

1. Silent sync on app start (default behavior)

Updates download silently and apply on next restart.

const MyApp = () => <App />;
export default codePush(MyApp);

2. Silent sync on every resume

const MyApp = () => <App />;

export default codePush({
checkFrequency: codePush.CheckFrequency.ON_APP_RESUME,
installMode: codePush.InstallMode.ON_NEXT_RESUME
})(MyApp);

3. Interactive with prompt

Prompts user before downloading. Mandatory updates show notification but can't be ignored.

const MyApp = () => <App />;

export default codePush({
updateDialog: true,
installMode: codePush.InstallMode.IMMEDIATE
})(MyApp);

4. With progress tracking

const MyApp = () => {
const codePushStatusDidChange = (status) => {
const messages = {
[codePush.SyncStatus.CHECKING_FOR_UPDATE]: "Checking for updates.",
[codePush.SyncStatus.DOWNLOADING_PACKAGE]: "Downloading package.",
[codePush.SyncStatus.INSTALLING_UPDATE]: "Installing update.",
[codePush.SyncStatus.UP_TO_DATE]: "Up-to-date.",
[codePush.SyncStatus.UPDATE_INSTALLED]: "Update installed.",
};
console.log(messages[status] || "Unknown status");
};

const codePushDownloadDidProgress = ({ receivedBytes, totalBytes }) => {
console.log(`${receivedBytes} of ${totalBytes} received.`);
};

return <App />;
};

// Attach hooks as static properties
MyApp.codePushStatusDidChange = codePushStatusDidChange;
MyApp.codePushDownloadDidProgress = codePushDownloadDidProgress;

export default codePush(MyApp);

CodePushOptions

OptionTypeDefaultDescription
checkFrequencyCheckFrequencyON_APP_STARTWhen to check for updates
deploymentKeyStringFrom configOverride deployment key at runtime
installModeInstallModeON_NEXT_RESTARTWhen to install optional updates
mandatoryInstallModeInstallModeIMMEDIATEWhen to install mandatory updates
minimumBackgroundDurationNumber0Seconds in background before restart (for ON_NEXT_RESUME/ON_NEXT_SUSPEND)
updateDialogUpdateDialogOptionsnullConfigure update prompt dialog
rollbackRetryOptionsRollbackRetryOptionsnullConfigure rollback retry behavior

UpdateDialogOptions

OptionDefaultDescription
appendReleaseDescriptionfalseAppend release description to notification
descriptionPrefix" Description: "Prefix for release description
mandatoryContinueButtonLabel"Continue"Button text for mandatory updates
mandatoryUpdateMessage"An update is available that must be installed."Message for mandatory updates
optionalIgnoreButtonLabel"Ignore"Ignore button text
optionalInstallButtonLabel"Install"Install button text
optionalUpdateMessage"An update is available. Would you like to install it?"Message for optional updates
title"Update available"Dialog title

RollbackRetryOptions

The rollback retry mechanism allows the application to attempt to reinstall an update that was previously rolled back (with the restrictions specified in the options).

OptionDefaultDescription
delayInHours24Minimum hours before retrying rolled-back update
maxRetryAttempts1Maximum retry attempts (min: 1)

Event Hooks

codePushStatusDidChange

Called when sync moves between stages. Receives a SyncStatus code.

codePushDownloadDidProgress

Called periodically during download with:

  • totalBytes - Total bytes to download
  • receivedBytes - Bytes downloaded so far

codePush.allowRestart

codePush.allowRestart(): void;

Re-allows restarts after disallowRestart. If an update was pending restart, it triggers immediately.

Won't trigger restart if:

  • No updates installed since disallowRestart
  • Pending update uses ON_NEXT_RESTART mode
  • Pending update uses ON_NEXT_RESUME but app hasn't backgrounded yet
  • No restartApp calls since disallowRestart

codePush.checkForUpdate

codePush.checkForUpdate(
deploymentKey?: string,
handleBinaryVersionMismatchCallback?: (update: RemotePackage) => void
): Promise<RemotePackage | null>;

Checks if an update is available.

Returns null when:

  • No releases in deployment
  • Latest release targets different binary version
  • App already has latest release
  • Latest release is disabled
  • Device not in active rollout percentage

Returns RemotePackage when update is available.

const update = await codePush.checkForUpdate();

if (!update) {
console.log("The app is up to date!");
} else {
console.log("An update is available! Should we download it?");
}

The optional handleBinaryVersionMismatchCallback notifies when binary updates are available (e.g., installed 1.0.1 but 1.0.2 exists).

App Store Note

Be cautious using Alerts in this callback for iOS apps due to App Store guidelines about forced actions.


codePush.disallowRestart

codePush.disallowRestart(): void;

Temporarily blocks restarts from:

  1. Updates with InstallMode.IMMEDIATE
  2. Updates with ON_NEXT_RESUME when app resumes
  3. Direct restartApp calls

Useful for protecting critical flows like onboarding.

const OnboardingProcess = () => {
useEffect(() => {
// Block restarts during onboarding
codePush.disallowRestart();

return () => {
// Re-allow on unmount (triggers pending restart if any)
codePush.allowRestart();
};
}, []);

return <OnboardingContent />;
};

codePush.getUpdateMetadata

codePush.getUpdateMetadata(
updateState: UpdateState = UpdateState.RUNNING
): Promise<LocalPackage | null>;

Gets metadata for installed updates matching the specified state.

Returns null when:

  • No CodePush updates installed yet
  • Binary update cleared old CodePush updates
  • RUNNING requested but no CodePush update is running
  • PENDING requested but no pending updates
// Get running update info
const update = await codePush.getUpdateMetadata();
if (update) {
analytics.track({ CodePushRelease: update.label });
}

// Check for pending updates
const pending = await codePush.getUpdateMetadata(codePush.UpdateState.PENDING);
if (pending) {
// Maybe force restart?
}

codePush.notifyAppReady

codePush.notifyAppReady(): Promise<void>;

Marks the current update as successful, preventing automatic rollback on next restart.

tip

If using sync, this is called automatically. Only needed for manual update flows.


codePush.restartApp

codePush.restartApp(onlyIfUpdateIsPending?: boolean): void;

Immediately restarts the app. If onlyIfUpdateIsPending is true, only restarts if there's a pending update.

Useful when using ON_NEXT_RESTART/ON_NEXT_RESUME modes and you have a good moment to apply updates (e.g., user returns to home screen).


codePush.sync

codePush.sync(
options?: SyncOptions,
syncStatusChangeCallback?: (status: SyncStatus) => void,
downloadProgressCallback?: (progress: DownloadProgress) => void,
handleBinaryVersionMismatchCallback?: (update: RemotePackage) => void
): Promise<SyncStatus>;

All-in-one method: checks, downloads, and installs updates.

Two modes:

  1. Silent (default) - Downloads silently, applies on next restart
  2. Active - Prompts user, applies immediately
// Silent update
codePush.sync();

// Active with prompt
codePush.sync({
updateDialog: true,
installMode: codePush.InstallMode.IMMEDIATE
});

SyncOptions

Same as CodePushOptions except checkFrequency.

// Different deployment key
codePush.sync({ deploymentKey: "KEY" });

// Install on resume after 5 min in background
codePush.sync({
installMode: codePush.InstallMode.ON_NEXT_RESUME,
minimumBackgroundDuration: 300
});

// Optional on restart, mandatory on resume
codePush.sync({
mandatoryInstallMode: codePush.InstallMode.ON_NEXT_RESUME
});

// Custom dialog
codePush.sync({
updateDialog: {
title: "An update is available!",
appendReleaseDescription: true,
descriptionPrefix: "\n\nChange log:\n"
},
installMode: codePush.InstallMode.IMMEDIATE
});

With callbacks

codePush.sync(
{ updateDialog: true },
(status) => {
if (status === codePush.SyncStatus.DOWNLOADING_PACKAGE) {
showDownloadModal();
} else if (status === codePush.SyncStatus.INSTALLING_UPDATE) {
hideDownloadModal();
}
},
({ receivedBytes, totalBytes }) => {
updateProgress(receivedBytes / totalBytes);
}
);

Return values

StatusValueDescription
UP_TO_DATE0App is up-to-date
UPDATE_INSTALLED1Update installed, will apply based on InstallMode
UPDATE_IGNORED2User ignored optional update
SYNC_IN_PROGRESS4Another sync is already running

Package Objects

LocalPackage

Represents a downloaded/installed update. Get via getUpdateMetadata or RemotePackage.download.

Properties

PropertyTypeDescription
appVersionStringTarget binary version
deploymentKeyStringDeployment key used
descriptionStringRelease description
failedInstallBooleanWas this update rolled back before?
isFirstRunBooleanFirst run after install?
isMandatoryBooleanIs mandatory update?
isPendingBooleanInstalled but not yet applied?
labelStringInternal label (e.g., v5)
packageHashStringSHA hash
packageSizeNumberSize in bytes

Methods

await localPackage.install(
installMode?: InstallMode, // default: ON_NEXT_RESTART
minimumBackgroundDuration?: number // default: 0
): Promise<void>;

RemotePackage

Represents an available update on the server. Get via checkForUpdate.

Inherits all LocalPackage properties plus:

PropertyTypeDescription
downloadUrlStringURL to download the update

Methods

const localPackage = await remotePackage.download(
downloadProgressCallback?: (progress: DownloadProgress) => void
): Promise<LocalPackage>;

Enums

InstallMode

ValueCodeDescription
IMMEDIATE0Install and restart immediately
ON_NEXT_RESTART1Install now, apply on natural restart (default)
ON_NEXT_RESUME2Install now, apply when app resumes from background
ON_NEXT_SUSPEND3Install while in background (after minimumBackgroundDuration)

CheckFrequency

ValueCodeDescription
ON_APP_START0Check on app process start
ON_APP_RESUME1Check when app returns from background
MANUAL2Only check when sync() is called

SyncStatus

ValueCodeDescription
UP_TO_DATE0App is up-to-date
UPDATE_INSTALLED1Update installed
UPDATE_IGNORED2User ignored optional update
UNKNOWN_ERROR3Unknown error occurred
SYNC_IN_PROGRESS4Another sync is running
CHECKING_FOR_UPDATE5Querying server
AWAITING_USER_ACTION6Waiting for user response to dialog
DOWNLOADING_PACKAGE7Downloading update
INSTALLING_UPDATE8Installing update

UpdateState

ValueCodeDescription
RUNNING0Currently running update
PENDING1Installed but pending restart
LATEST2Latest available (running or pending)