State Management Methods
Reactter provides several methods to manage states in a more efficient way. These methods are used to create and manipulate states in a more organized way.
Rt.lazyState
Rt.lazyState
is a method that allows to create a state lazily in a dependency(class registered via the dependency injection).
It’s useful when you want to create a state that depends on other states or variables.
Rt.lazyState
is generally used in states declared with the late
keyword.
Syntax
Example
For example, you can create a state lazily in a class like this:
and then use it like this:
In the example, the uCount
state is lazily declared inside the CountController
class using Rt.lazyState
. It’s accessed after creating the CountController
instance, and when the instance is deleted, the state is automatically disposed, ensuring efficient resource management.
Rt.batch
The Rt.batch
method allows multiple state changes to be grouped together, triggering them all at the end when the callback
completes. This ensures that associated side effects occur only once, improving performance and reducing unnecessary re-renders.
Syntax
Example
In this example, the UserController
class demonstrates how to use the Rt.batch
method to manage state updates efficiently.
By using Rt.batch
, the updates to uName
and uAge
are grouped together, ensuring that the UseEffect
hook’s callback is triggered only once after both state changes have been made.
This improves performance by reducing unnecessary re-renders and ensuring that side effects, like printing the updated values, occur only once per batch of state changes.
Rt.untracked
The Rt.untracked
method allows you to perform multiple state changes that are not tracked by the Reactter framework.
This means that changes made using this method won’t trigger associated side effects or re-renders typically observed in reactive state management systems.
Syntax
Example
In this example, the UserController
class demonstrates how to use the Rt.untracked
method to update the states without triggering the associated side effects.
By using Rt.untracked
, the updates to uName
and uAge
are made without triggering the associated side effects, ensuring that the UseEffect
hook’s callback is not triggered after these state changes.
This is useful when you want to update states without causing re-renders or side effects, e.g., when initializing states or performing batch updates.