目录

Controlling State Update

In the declarative UI programming paradigm, the UI is a function of an application state, and a modification of the application state updates a corresponding UI. ArkUI uses the MVVM pattern, where ViewModel is used to bind data and views. When data is updated, the views are updated. The following figure shows the effect.

Figure 1 MVVM pattern of ArkUI

ArkUI provides a series of decorators, such as @Prop, @Link, @Provide, and LocalStorage, to implement the capabilities of ViewModel. When a variable in a custom component is decorated by a decorator, the variable becomes a state variable. The change of the state variable triggers the UI rendering refresh.

Avoiding Unnecessary State Variables

For details, see Avoiding Unnecessary State Variables.

Minimizing the State Sharing Scope

Reducing Unnecessary Level-by-Level Parameter Passing Operations

When the levels of components that share the state differ greatly, the state is passed on level by level. For all components involved in state passing, input parameters need to be added to receive the state and then pass the state to child components. Therefore, you need to reduce unnecessary level-by-level parameter passing operations and select decorators based on state complexity.

Splitting Complex States in Refined Mode

AppStorage boasts the widest application scope. To facilitate development, you can easily store various states in AppStorage for sharing, which usually results in a large amount of performance loss. For details about use cases, see Splitting Complex States in Refined Mode.

Modifying Logic for States in a Centralized Manner

If the logic for modifying the state of multiple child components is similar, it is recommended that the state be modified in a single function to improve the reusability of the logic as well as the maintainability and testability of the code. For details about use cases, see Modifying Logic for States in Centralized Manner.

Accurately Controlling Component Refresh by Listening and Subscription

When multiple components depend on the same data source, direct association with the data source will trigger the update of all components each time the data source changes. The following policies can be used to accurately control component updates.