Class: Snack

January 27, 2026 · View on GitHub

snack-sdk

Class: Snack

Hierarchy

  • Snack

Index

Constructors

Methods

Constructors

constructor

+ new Snack(options: SnackOptions): Snack

Parameters:

NameType
optionsSnackOptions

Returns: Snack

Methods

addLogListener

addLogListener(listener: SnackLogListener): SnackListenerSubscription

Adds a callback for listening for any client generated log messages.

example

const unsubscribe = Snack.addLogListener((log) => {
  console.log('log message received: ', log);
});

unsubscribe(); // Remove listener

Parameters:

NameType
listenerSnackLogListener

Returns: SnackListenerSubscription


addStateListener

addStateListener(listener: SnackStateListener): SnackListenerSubscription

Adds a callback for listening for any state changes.

example

const unsubscribe = Snack.addStateListener((state, prevState) => {
  if (state.name !== prevState.name) {
    console.log('name changed: ', state.name);
  }
});

Snack.setName('unforgiven orange'); // // Make a change to the state

unsubscribe(); // Remove listener

Parameters:

NameType
listenerSnackStateListener

Returns: SnackListenerSubscription


getDownloadURLAsync

getDownloadURLAsync(saveOptions?: SnackSaveOptions): Promise<string>

Gets the URL at which the Snack can be downloaded as a zip file. Will automatically save the Snack if it contains unsaved changes.

Parameters:

NameType
saveOptions?SnackSaveOptions

Returns: Promise<string>


getPreviewAsync

getPreviewAsync(): Promise<SnackConnectedClients>

Requests a preview from the connected clients.

The previews are returned in the previewURL field of each connectedClient.

Returns: Promise<SnackConnectedClients>


getState

getState(): SnackState

Returns the current state of the Snack. This includes files, dependencies and other meta-data about the Snack.

Returns: SnackState


getStateAsync

getStateAsync(): Promise<SnackState>

Waits for any pending operations such as running dependencies resolutions before returning the state.

Returns: Promise<SnackState>


reloadConnectedClients

reloadConnectedClients(): void

Reloads all connected clients.

Note: During the reload proces, clients may get disconnected which causes the connectedClient to disappear and re-appear. The reloadTimeout option in the constructor can be used to keep connectedClients "alive" during the reload process.

Returns: void


saveAsync

saveAsync(options?: SnackSaveOptions): Promise<{ hashId: undefined | string ; id: string ; url: string = saveURL ; snackId: undefined | string ; accountSnackId: undefined | string ; }>

Uploads the current code to Expo's servers and return a url that points to that version of the code.

Parameters:

NameType
options?SnackSaveOptions

Returns: Promise<{ hashId: undefined | string ; id: string ; url: string = saveURL ; snackId: undefined | string ; accountSnackId: undefined | string ; }>


sendCodeChanges

sendCodeChanges(): void

Sends any pending code changes to the connected clients. No changes are send if all clients are already up to date.

Returns: void


setCodeChangesDelay

setCodeChangesDelay(delay: number): void

Sets the delay that is used before sending code updates to the connected clients. Use this method to set the "debounce" timeout to use for sending code changes over the transport.

  -1 = Disable automatic sending of code changes (use `sendCodeChanges` to trigger the send)
   0 = Code changes are sent immediately to the connected clients
1..n = Code changes are debounced and sent after the wait time

Parameters:

NameTypeDescription
delaynumberTimeout in milliseconds (or -1 to disable automatic code updates)

Returns: void


setDescription

setDescription(description: string): void

Sets the description of the Snack.

Parameters:

NameType
descriptionstring

Returns: void


setDeviceId

setDeviceId(deviceId?: undefined | string): void

Sets the device-id of an Expo client. When set and online is true, causes this Snack to appear on the "Recently in Development" section of that Expo client.

Parameters:

NameType
deviceId?undefined | string

Returns: void


setDisabled

setDisabled(disabled: boolean): void

Enables or disables the Snack.

When disabled, no uploads or dependency resolve operations are performed.

Parameters:

NameType
disabledboolean

Returns: void


setFocus

setFocus(): void

Sets the focus to this Snack.

Causes this Snack to be moved to the top of the "Recently in Development" list in the Expo client.

Returns: void


setName

setName(name: string): void

Sets the name of the Snack.

Parameters:

NameTypeDescription
namestringE.g. "conspicious orange"

Returns: void


setOnline

setOnline(enabled: boolean): void

Makes the Snack available online.

When online, a snackpub channel is created to which clients can connect.

Parameters:

NameType
enabledboolean

Returns: void


setSDKVersion

setSDKVersion(sdkVersion: SDKVersion): void

Sets the Expo SDK version.

Parameters:

NameTypeDescription
sdkVersionSDKVersionValid SDK version (e.g. "38.0.0")

Returns: void


setUser

setUser(user?: SnackUser): void

Sets the associated user account.

When set and online is true, causes this Snack to appear on the "Recently in Development" section of all Expo clients that are signed in with that account.

Parameters:

NameType
user?SnackUser

Returns: void


updateDependencies

updateDependencies(dependencies: { [name:string]: SnackDependency | null; }): void

Updates dependencies.

Use this method to add/remove/update dependencies. To remove a dependency specify null as the value of the key/value pair.

example

const Snack = new Snack({
  dependencies: {
    'react-native-paper': '~2.0.0'
  }
});

// Add dependency
Snack.updateDependencies({
  'expo-font': '9.0.0'
});

// Remove dependency
Snack.updateDependencies({
  'expo-font': null
});

Parameters:

NameType
dependencies{ [name:string]: SnackDependency | null; }

Returns: void


updateFiles

updateFiles(files: { [path:string]: SnackFile | null; }): void

Updates code or asset files.

Use this method to add/remove/update files and upload assets. To remove a file specify null as the value of the file.

example

const Snack = new Snack({
  files: {
    'App.js': { type: 'CODE', contents: 'console.log("hello world!");' },
    'data.json': { type: 'CODE', contents: '{}' },
  }
});

// Add or update files
Snack.updateFiles({
  'App.js': {
    type: 'CODE',
    contents: 'console.log("Hello Snack!");'
  }
});

// Upload an asset
Snack.updateFiles({
  'assets/logo.png': {
    type: 'ASSET',
    contents: file // File, Blob or FormData
  }
});

// Add a pre-uploaded asset
Snack.updateFiles({
  'assets/expo.jpg': {
    type: 'ASSET',
    contents: 'https://mysite/expo.jpg'
  }
});

// Remove files
Snack.updateFiles({
  'data.json': null,
  'assets/expo.jpg': null
});

Parameters:

NameType
files{ [path:string]: SnackFile | null; }

Returns: void


uploadAssetAsync

uploadAssetAsync(contents: File | Blob | FormData): Promise<string>

Helper function that uploads an asset and returns its url.

Parameters:

NameType
contentsFile | Blob | FormData

Returns: Promise<string>