Managing Component State
Many JourneyApps UI components maintain internal state that controls pagination, search, selection, sorting (where applicable), and more. The two components that offer the most comprehensive state management features are:
object-table— tabular data display with sorting, filtering, editing, selection, etc.list— card-style or simple item listing with pagination, search, and single selection.
Managing state is considered an advanced topic. In most cases, both components work very well using default behavior without any manual state control.
Shared Concepts
Both object-table and list support the same three main mechanisms for reading and controlling state:
on-state-changeCalled whenever the user interacts with the component in a way that changes its state (paging, searching, selecting, etc.).init-stateSets the initial state when the view is loaded (very useful for preserving user preferences when returning to a view).Programmatic
setState()via JavaScript/TypeScriptcomponent.objectTable({ id: "…" }).setState(…)orcomponent.list({ id: "…" }).setState(…)Allows updating state after the view has loaded (e.g. reset filters on button press, jump to page X, clear search).
When to prefer one method over the other
Use
init-statefor most cases — it runs once on view load and is perfect for remembering pagination/search/selection when users navigate back.Use
setState()only when you need to change state dynamically after the view has loaded (e.g. button resets table, syncs two components, etc.).
Supported components Currently only the following components expose this state management API:
Data displayed in an list and object-table is controlled by its state. The objects provided below list all of the properties which can be safely accessed and mutated from JavaScript/TypeScript.
Advanced topic
Managing state is considered an advanced topic. In most cases a component can be used effectively without directly managing its state.
on-state-change
on-state-changeThe function specified in on-state-change is called when the state of the table or the list is changed due to a UX/UI interaction such as changing to the next page. The $state parameter can be passed to the function which contains a simplified JavaScript/TypeScript object of the component's state.
The internal structure of $state is as follows:
The validationErrors field is available from Runtime 4.90.9+. It is handled internally and therefore cannot be set by the developer via the setState or init-state logic.
column properties: The state payload will in contain column properties which refer to the current name of the column. This is provided as convenience data to make working with the state easier, but it is not used when initializing state since the value can change dynamically. Using the column data to initialize the state is therefore considered unsafe and should not be relied on.
init-state
init-stateA table or list can initialize its internal state after the component is loaded using a similar structure as on-state-change.
How init-state differs from on-state-change:
For the
object-tablecomponents,init-statewill ignorecolumnfields (it only uses columnkeyinformation)
The internal structure of the returned state object is as follows:
Note: The validationErrors field is handled internally and cannot be set by the developer.
Changing the state from JavaScript/TypeScript
To change the state of the table or list using JavaScript/TypeScript, make use of the component.objectTable({ id: "my-id" }).setState({ ... }) method or component.list({ id: "my-id" }).setState({ ... }). This method requires an id="" attribute to be present on the object-table that matches the id used in above component.objectTable().
Note: The setState() method takes the exact same payload as init-state="".
For example:
When to use setState() vs init-state=""
Since both of these methods do the same thing but in different parts of an application, it is important to understand why and when you should use one over the other. In most JourneyApps applications, init-state="" is the easiest to use because it is called only once when the view loads. This is particularly useful when users navigate using a table, and when they return to the table they expect it to look exactly the same as before.
setState() is useful when a developer needs to control or manipulate the table after the view has loaded. Actions such as changing the page and updating the select cell are events that should not fire on every update on a view, but rather when very specific events occur, such as pressing a particular button.
Tip: Implementing setState() can slow down your view if done frequently. Use it only where init-state="" is not feasible.
Last updated