How to store and use values with States
In TeleportHQ, a state is a named value that a page or component remembers and can change while a visitor uses it, for example isMenuOpen (true or false), activeTab (a number) or selectedCategory (a text). Elements can show or hide based on a state, lists can filter or sort by it, and clicks can change it. Create one from the Page settings button above the top right of the canvas (Component settings inside a component), in the State tab. States work in every TeleportHQ project, website or web app.
Before you start
- Nothing to turn on. Page and component states work in every project. They need no advanced feature, and using them doesn't turn a website into a web app.
- Global states (one value shared by every page and component) are different: they need a web app. See Create a global state.
- Role. You need a role that can edit components, for example Designer. The Page settings button is hidden for other roles and in read-only projects.
Create a state for a page or component
- Open the page on the canvas, for example your homepage. For a component, open the component instead.
- Above the top right of the canvas, click Page settings (Component settings in a component).
- Click the State tab.
Click Create new state. The New State Value panel opens beside it.
- In Name, type a name, for example
activeTab. - Under Source, keep Static value ("Set a fixed default value").
- In Type, choose string (text), boolean (true or false), number, object or array (a list). For example, choose number.
- In Default, type the value visitors start with, for example
1. - Click Create.
The state appears in the table with its Name, Type, Default value and Internal value.
Fill a state from your data
Under Source, a TeleportHQ state can also start with what a data source returns, for example the five newest products:
- Data source ("Populate from a database, API or file"): in Data Source, choose the source. Type and Default disappear: the state holds what the source returns.
- For a database, such as the TeleportHQ database, choose the table in Collection/Table. The state holds the table's rows as a list (array).
- For a REST API, a JavaScript list, a CSV file or a Google Sheet, the Read from field ("What the state holds from this data source") reads the source first ("Reading the data source…"), then lets you pick: All rows (CSV file, Google Sheet), Whole response (REST API, JavaScript), or a list or object inside it, for example
items · list of 12 items. The state takes the type of what you pick.
- SQL query ("Write a custom SELECT query"): choose a database and type a query, for example
SELECT name, price FROM products ORDER BY price LIMIT 5. Only reading is allowed: "Only SELECT (and WITH ... SELECT) queries are allowed. INSERT, UPDATE, DELETE, and all schema changes are blocked." A query only runs on a database, so switching to SQL query after picking a REST API, JavaScript, CSV or Google Sheets source clears that pick.
Show such a state with a list. See How to show a list of items. For a state filled from a data source, the code button in its row (tooltip Edit mapping function) lets you reshape what it holds with a short JavaScript function, for example to feed a chart.
On your published site, a state filled from data is read again on every visit, so new and changed records show on the next page load, without a new publish. When a visitor opens another record of the same page, the state is read again for that record. See Do page-load workflows run again when a visitor opens another record of the same page?.
Use a state on your page
- Show or hide elements: set a condition such as
activeTab=2. See How to show or hide elements based on a condition. - Change it on a click: select a button, open the Events tab in the right panel, and add an event of type Change State: pick the state in Modifies and the new value in To. See How to use states in a component.
- Show its value: click the plug icon next to a text and pick the state under state in the panel that opens.
- Filter or sort a list: bind a filter's value, or Sort by, to the state. See How to filter a list and How to sort a list.
Create a global state
A TeleportHQ global state works on every page and component, for example a cartCount shown in the header. Global states need a web app.
- At the bottom of the left sidebar, click the Project settings cog.
- Under Data & code, click Global state.
- Click New global state, fill in the name, type and starting value, and confirm.
On a website, the settings are titled Global state needs web app features ("Global state holds values every page and component can read: the signed-in user, a cart, a chosen theme.") and show Enable Web App features. See What is a web app in TeleportHQ? before you click it.
On a web app, below your own global states, Platform globals lists the built-in, read-only values your project's features provide, such as Current User or Cart. Click one to open its details ("Platform global · read-only"): How to use it and the Properties it holds.
Check that it worked
The state is listed in the State tab (or under Global states). The Internal value column holds the value the editor uses for its preview ("This is the value used inside the editor, for conditional rendering"): change it to see how elements react on the canvas. Click Preview in the bottom toolbar to try your clicks as a visitor would.
Troubleshooting
"This name is already used by another state. Local and global state names share one namespace — pick a different one." A state on this page or component, or a global state, already has that name. Choose another, for example productTab instead of activeTab.
"Select a data source", "Select what to read from the data source", "Write a SELECT query" or "Invalid query" The Data source or SQL query source isn't filled in yet, or the query has a mistake. Pick the source and what to read, or check the table and column names in the query.
"This data source returned nothing a state can hold." The source answered with nothing to keep, for example an empty response. Check the data source in the Data Sources panel, or choose another one. "Could not read this data source: …" means the source couldn't be reached.
I can't see the Page settings button Your role can't edit components, or the project is read-only. Ask the workspace owner or an admin.
My live site starts with the wrong value Visitors start with the Default value. The Internal value only changes the editor's preview.
Good to know
- State names start with a letter or
_, use letters, numbers,_and-, and are up to 30 characters long. - Downloaded code: the Next.js download runs states. The HTML download keeps simple uses, such as a click that toggles a state to open a menu; anything it can't run shows as the page first loads. Global states don't work in the HTML download. See How to download your site as code.
- Ready-made widgets such as tabs, pop-ups (modals) and collapsible text use states for you, and also work on websites.