1e41f4b71Sopenharmony_ci# State Management Overview 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciIn previous examples, most of the pages built are static pages, which are delivered to the end user without having to be processed. If you are building dynamic, interactive pages, you need to master state management. 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci **Figure 1** State managed UI 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ciIn the preceding example, the interaction between the user and the application triggers an update in the text state, which in turn triggers re-rendering of the UI. As a result, the **Hello World** text changes to **Hello ArkUI**. 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ciIn the declarative UI framework, the UI is the execution result of the application state. You build a UI model in which the application runtime state is a parameter. When the parameter is changed, the UI as the return result is updated accordingly. This process of UI re-rendering caused by the application runtime state changes is called the state management mechanism in ArkUI. 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ciCustom components have variables. A variable must be decorated by a decorator whenever the re-rendering of the UI depends on this variable. Otherwise, the UI is rendered only at initialization and will not be updated. The following figure shows the relationship between the state and view (UI). 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci- View (UI): UI rendering, which is visual representation of the UI description in the **build** method and \@Builder decorated method. 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci- State: data that drives the UI to re-render. State data is changed by invoking the event method of the component. The change of the state data triggers the re-rendering of the UI. 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci## Basic Concepts 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci- State variable: a variable decorated by a state decorator. Its value change will trigger UI re-renders. Example: @State num: number = 1, where @State is a state decorator and **num** is a state variable. 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci- Regular variable: a variable that is not decorated by a state decorator and is usually used for auxiliary calculation. Its value change will not trigger UI re-renders. In the following example, the **increaseBy** variable is a regular variable. 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci- Data source/Synchronization source: original source of a state variable, which can be synchronized to different state data. Generally, it is the data passed from the parent component to the child component. In the following example, the data source is **count: 1**. 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci- Named parameter mechanism: a mechanism where the parent component passes state variables to the child component by specifying parameters. It is the primary means of passing synchronization parameters from the parent component to the child component. Example: **CompA({ aProp: this.aProp })**. 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci- Initialization from the parent component: a process where the parent component uses the named parameter mechanism to pass specified parameters to the child component. The default value used in local initialization will be overwritten by the value passed from the parent component. Example: 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci ```ts 42e41f4b71Sopenharmony_ci @Component 43e41f4b71Sopenharmony_ci struct MyComponent { 44e41f4b71Sopenharmony_ci @State count: number = 0; 45e41f4b71Sopenharmony_ci private increaseBy: number = 1; 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci build() { 48e41f4b71Sopenharmony_ci } 49e41f4b71Sopenharmony_ci } 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci @Component 52e41f4b71Sopenharmony_ci struct Parent { 53e41f4b71Sopenharmony_ci build() { 54e41f4b71Sopenharmony_ci Column() { 55e41f4b71Sopenharmony_ci // Initialization from the parent component: The named parameter specified here will overwrite the default value defined locally. 56e41f4b71Sopenharmony_ci MyComponent({ count: 1, increaseBy: 2 }) 57e41f4b71Sopenharmony_ci } 58e41f4b71Sopenharmony_ci } 59e41f4b71Sopenharmony_ci } 60e41f4b71Sopenharmony_ci ``` 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci- Child component initialization: a capability to pass state variables to the child component to initialize the corresponding state variables therein. The example is the same as above. 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci- Local initialization: a process where a value is assigned to a variable as its default value in the variable declaration. Example: \@State count: number = 0. 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci## State Management (V1 Stable Version) 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ciState management V1 provides a complete set of features for you to manage state in your application. 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci### Decorator Overview 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ciArkUI state management V1 provides a diverse array of decorators. You can use these decorators to observe state variables changes within a component or globally and pass the changes between different component levels (for example, between parent and child components or grandparent and grandchild components). According to the scope of the state variable, decorators can be roughly classified into the following types: 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci- Decorators for managing the state of a component: implement state management at the component level by allowing for observation of state changes within a component and changes at different component levels. The observation is limited to state changes in the same component tree, that is, on the same page. 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci- Decorators for managing the state of an application: implement global state management of an application by allowing for observation of state changes on different pages or even different UIAbility components. 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ciAccording to the data transfer mode and synchronization type, decorators can also be classified into the following types: 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci- Decorators that allow for one-way (read-only) transfer 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci- Decorators that allow for two-way (mutable) transfer 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ciThe following figure illustrates the decorators. For details, see [Component State Management](arkts-state.md) and [Application State Management](arkts-application-state-management-overview.md). You can use these decorators at your disposal to implement linkage between data and the UI. 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ciIn the preceding figure, the decorators in the Components area are used for state management at the component level, while others are used for state management at the application level. You can use [@StorageLink](arkts-appstorage.md#storagelink)/[@LocalStorageLink](arkts-localstorage.md#localstoragelink) to implement two-way synchronization of the application and component state, and [@StorageProp](arkts-appstorage.md#storageprop)/[@LocalStorageProp](arkts-localstorage.md#localstorageprop) to implement one-way synchronization. 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ciDecorators for [component state management](arkts-state.md): 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci- [\@State](arkts-state.md): An \@State decorated variable holds the state of the owning component. It can be the source of one- or two-way synchronization with child components. When the variable changes, the dependent component will be updated. 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci- [\@Prop](arkts-prop.md): An \@Prop decorated variable can create one-way synchronization with a variable of its parent component. \@Prop decorated variables are mutable, but changes are not synchronized to the parent component. 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci- [\@Link](arkts-link.md): An \@Link decorated variable creates two-way synchronization with a variable of its parent component. When the @Link decorated variable has its value changed, its source is updated as well; when the source updates, the @Link decorated variable will do as well. 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci- [\@Provide/\@Consume](arkts-provide-and-consume.md): Variables decorated by \@Provide/\@Consume are used for data synchronization across component levels. The components can be bound to the variables through aliases or attribute names. Data does not need to be passed through the named parameter mechanism. 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ci- [\@Observed](arkts-observed-and-objectlink.md): \@Observed is a class decorator. You can use it to decorate the class that has multiple levels of nested objects or arrays to be observed. Note that \@Observed must be used with \@ObjectLink for two-way synchronization or with \@Prop for one-way synchronization. 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci- [\@ObjectLink](arkts-observed-and-objectlink.md): An \@ObjectLink decorated variable is used with an \@Observed decorated class of the parent component for two-way data synchronization. It is applicable in scenarios involving multiple levels of nested objects or arrays in the class. 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci> **NOTE** 113e41f4b71Sopenharmony_ci> 114e41f4b71Sopenharmony_ci> Only [\@Observed/\@ObjectLink](arkts-observed-and-objectlink.md) can be used to observe changes of nested attributes. Other decorators can be used to observe changes of attributes at the first layer only. For details, see the "Observed Changes and Behavior" part in each decorator section. 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ciDecorators for [application state management](arkts-application-state-management-overview.md): 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci- [AppStorage](arkts-appstorage.md): a special [LocalStorage](arkts-localstorage.md) singleton instance. It is an application-wide database bound to the application process and can be linked to components through the [@StorageProp](arkts-appstorage.md#storageprop) and [@StorageLink](arkts-appstorage.md#storagelink) decorators. 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci- AppStorage is the hub for application state. Data that needs to interact with components (UI) is stored in AppStorage, including [PersistentStorage](arkts-persiststorage.md) and [Environment](arkts-environment.md) data. The UI accesses the data through the decorators or APIs provided by AppStorage. 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci- LocalStorage: an in-memory "database" for the application state declared by the application and typically used to share state across pages. It can be linked to the UI through the [@LocalStorageProp](arkts-localstorage.md#localstorageprop) and [@LocalStorageLink](arkts-localstorage.md#localstoragelink) decorators. 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci### Other Features in State Management V1 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci[\@Watch](arkts-watch.md): listens for the changes of state variables. 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci[$$operator](arkts-two-way-sync.md): provides a TS variable by-reference to a built-in component so that the variable value and the internal state of that component are kept in sync. 133e41f4b71Sopenharmony_ci## State Management (V2 Trial Version) 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ciState management V2 adds new features, such as in-depth observation and attribute-level update, to state management V1. 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci>**NOTE** 138e41f4b71Sopenharmony_ci> 139e41f4b71Sopenharmony_ci>State management V2 is still under development, and some features may be incomplete or not always work as expected. 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci### How State Management V2 Stacks Against State Management V2 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ciState management V1 uses a proxy to observe data. When a state variable is created, a data proxy observer is also created. The observer can sense the proxy change but cannot sense the actual data change. Therefore, the observer has the following restrictions: 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci- State variables cannot exist independently of the UI. When the same data is proxied by multiple views, the change in one view cannot be synchronized to other views. 146e41f4b71Sopenharmony_ci- Only the changes at the first layer of object attributes can be sensed, and in-depth observation and listening are not available. 147e41f4b71Sopenharmony_ci- Redundant re-renders exist in scenarios where attributes in an object are changed or elements in an array are changed. 148e41f4b71Sopenharmony_ci- There are many restrictions on the use of decorators. The input and output of state variables are not specified in components, which is inconvenient for componentization. 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ciState management V2 enhances the observation on the data itself. The data itself is observable. This means that changing the data triggers the update of the corresponding view. Compared with state management V1, state management V2 has the following advantages: 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci- State variables are independent of the UI. Changing data triggers the update of the corresponding view. 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci- In-depth observation and listening on objects is supported, and the in-depth observation mechanism does not affect the observation performance. 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci- Objects can be updated at the attribute level, and arrays at the element level. 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci- The decorators are easier to use and more scalable. The input and output are specified in components, which facilitates componentization. 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ci### Decorator Overview 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ciState management (V2 Trial) offers the following decorators: 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ci- [\@ObservedV2](arkts-new-observedV2-and-trace.md): \@ObservedV2 allows for in-depth observation on the decorated classes. \@ObservedV2 and \@Trace are used in pairs to enable in-depth observation of properties in a class. 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci- [\@Trace](arkts-new-observedV2-and-trace.md): \@Trace is used to decorate a property in an \@ObservedV2 class, enabling the property to be observed. 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci- [\@ComponentV2](arkts-new-componentV2.md): In structs decorated by \@ComponentV2, the following decorators can be used: \@Local, \@Param, \@Event, \@Once, \@Monitor, \@Provider and \@Consumer. 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci- [\@Local](arkts-new-local.md): An \@Local decorated variable is internal state of the component and cannot be initialized externally. 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci- [\@Param](arkts-new-param.md): An \@Param decorated variable is used as the input of the component and can be initialized and synchronized externally. 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ci- [\@Once](arkts-new-once.md): An \@Once decorated variable is synchronized only once during initialization and must be used together with \@Param. 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci- [\@Event](arkts-new-event.md): An \@Event decorated method, as component output, can be used to affect variables in the parent component. 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci- [\@Monitor](arkts-new-monitor.md): \@Monitor is used in custom components decorated by \@ComponentV2 or classes decorated by \@ObservedV2 to implement in-depth observation on state variables. 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci- [\@Provider and \@Consumer](arkts-new-Provider-and-Consumer.md): These decorators are used to implement two-way synchronization across component levels. 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci- [\@Computed](arkts-new-Computed.md): An \@Computed decorated method is a computed property, for which computation is performed only once when the value changes. It is mainly used to solve the performance problem caused by repeated computation when the UI reuses the property multiple times. 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci- [!! Syntax](arkts-new-binding.md): The **!!** syntax is syntax sugar used for two-way binding. 187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ci### Constraints 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ciThe implementation of state management V2 is different from that of state management V1. As such, the two state management frameworks cannot be used together, and the following are not allowed: 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ci- Passing the variables decorated by decorators in V2to the variables decorated by decorators in V1 193e41f4b71Sopenharmony_ci- Using the [\@Observed](arkts-observed-and-objectlink.md) and [\@Track](arkts-track.md) decorators in V1 with the [\@ObservedV2](arkts-new-observedV2-and-trace.md) and [\@Trace](arkts-new-observedV2-and-trace.md) decorators in V2 194e41f4b71Sopenharmony_ci- Using V1 decorators (\@State, \@Prop, \@Link, \@ObjectLink, \@Provide, \@Consume, \@StorageLink, \@StorageProp, \@LocalStorageLink, \@LocalStorageProp, or \@Watch) in custom components decorated by \@ComponentV2 195e41f4b71Sopenharmony_ci- Using V2 decorators (\@Local, \@Param, \@Once, \@Event, \@Monitor, \@Provider, or \@Consumer) in custom components decorated by \@Component 196e41f4b71Sopenharmony_ci- Using \@Component and \@ComponentV2 at the same time 197e41f4b71Sopenharmony_ci- Using variable decorated by \@State, \@Prop, \@Link, \@Provide, \@Consume, \@StorageLink, \@StorageProp, \@LocalStorageLink, or \@LocalStorageProp in a custom component decorated by \@Component, when the type of the variable is an \@ObservedV2 decorated class 198e41f4b71Sopenharmony_ci- Using a variable decorated by \@Param, \@Local, \@Event, \@Provider(), or \@Consumer() in a custom component decorated by \@ComponentV2, when the type of the variable is an \@Observed decorated class 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_ciUsing a V2 decorator with a V1 decorator may cause undefined behavior, including redundant re-renders and loss of the in-depth observation, attribute observation, or property-level update capability. 201e41f4b71Sopenharmony_ci<!--no_check-->