Store temporary data with variables and localStorage
Learn how to temporarily store data for apps.
You're viewing documentation for classic apps, which use Retool's original app editor. The AI app builder is the recommended way to build apps and where Retool focuses new development.
You can store data temporarily instead of writing changes back to a data source. There are three methods available:
- Variables: Temporarily store data within the app.
- localStorage: Temporarily store data in the customer's browser.
- URL query strings: Temporarily store data in the URL.
Variables are like scratch-pads for your app. You can store data in them while your app is running. They are accessible anywhere on your app inside {{ }}, and they support reads ({{ variable.value }}), as well as writes ({{ variable.setValue(3) }}).
localStorage is a flexible solution for temporarily storing data locally to the browser. All apps in your Retool organization have the same origin and share access to its localStorage. Users can erase their localStorage at any time though or switch to a different browser.
URL query strings are best suited for smaller pieces of information that you need to share between apps. You can append query strings to an app's URL and retrieve them using the url object.
Store data using variables
Variables are reset each time your app is loaded.
Variables are useful in the following situations:
- Retaining input field values or query results that can change while a user interacts with your app.
- Storing data that is only used in the app session and doesn't need to persist.
- Performing complex logic that requires temporarily storing data first.
Create a variable
Open the code list and click + to create variables.
JavaScript methods
You can call the following methods within JavaScript queries.
variable.setValue(value: any)
Sets variableName.value to the value passed in as an argument. Returns a void Promise.
For example, to set the value of variable1 to a string "user_123_456":
await variable1.setValue("user_123_456");
variable.setIn(path, value: any)
Sets the value of variableName.value at a specified location. path accepts an array of keys or indexes to select, where each item represents a key or index in the path to the object to update. The last item of the path array is the key or index to update. The value is set without destroying and recreating other values.
When using variableName.setIn, the initial value of variableName must be an empty object (e.g., { }).
The following examples show objects before and after calls to variableName.setIn.
Path as an array of indexes
- Variable before
- Call to variable.setIn()
- Variable after
"object1": {
"key1": [
"value1",
"value2"
],
"key2": {
"nestedKey1": "value3"
}
}
await variableName.setIn(["object1", "key1", 1], "value3")`
"object1": {
"key1": [
"value1",
"value3"
],
"key2": {
"nestedKey1": "value3"
}
}