Skip to main content
Version: Next

Taro.setStorage(option)

Stores data in the specified key in the local cache. This operation will overwrite the original content of the key. The data storage lifecycle is consistent with the Mini Program, that is, data is always available unless manually deleted by the user or automatically deleted after a certain period of time. The maximum size of data stored in a single key is 1 MB, and the total size for data storage is limited to 10 MB.

Reference

Type

(option: Option) => Promise<CallbackResult>

Parameters

Option

PropertyTypeRequiredDescription
dataanyYesContents to be stored can only be native types, dates, and objects that can be serialized via JSON.stringify.
keystringYesThe specified key in the local cache
complete(res: any) => voidNoThe callback function used when the API call completed (always executed whether the call succeeds or fails)
fail(res: any) => voidNoThe callback function for a failed API call
success(res: Result) => voidNoThe callback function for a successful API call

Sample Code

Taro.setStorage({
key:"key",
data:"value"
})
try {
Taro.setStorageSync('key', 'value')
} catch (e) { }

API Support

APIWeChat Mini-ProgramH5React Native
Taro.setStorage✔️✔️✔️