Compose 提升状态的场景

Where to hoist that state in Compose? 了解在 Jetpack Compose 中提升状态的方式和场景。

1. Composable functions

点击 Message 展示消息时间。

Not hoisting is a valid option.

这个 UI 逻辑例子不需要提升状态。

2. State hoisting

在下面的例子中,消息列表的状态需要提升。

Place state in the lowest common ancestor.

3. Plain state holder class

使用 state holder class 做状态提升,例如 LazyListState。

LazyListState 抽象了 scrollPosition 同时暴露了应用逻辑的方法,比如 scrollToItem()、scroll()、animateScrollToItem()。

4. Android architecture Components(AAC)ViewModel

使用 ViewModel 做状态提升。

5. Recap

Keep state as low as possible.

相关推荐
StarsGreen1 天前
Android系统相关命令介绍
android·windows
传奇开心果编程1 天前
【六大移动UI框架比较学与练】第4课:列表与滚动——懒加载、复用与状态保持实战对照
android·flutter·react native·ios·swiftui·移动开发·android jetpack
恋猫de小郭1 天前
Flutter 奇葩 Bug:错过一次合成,后续每帧的 Raster 都顺带变慢?
android·前端·flutter
三少爷的鞋1 天前
一段 Kotlin 协程代码,藏着 5 个问题:你能发现几个?
android
棒棒的唐1 天前
发一个用本地AI(openclaw)写的调用本地llama.cpp服务对话,并按指定要求识别图片的PHP类
android·php·llama
_祝你今天愉快1 天前
Android 事件传递机制:[二] 手写模拟事件分发
android
东坡大表哥2 天前
【Ubuntu】Ubuntu 20.04 LTS → 22.04 LTS 升级实录
android·linux·ubuntu
不要试图纠正别人2 天前
IdleHandler 一次都不跑,问题不在主线程忙
android·性能优化
booboosui2 天前
记忆装不下时谁删:500 条上限与每回合十行索引
android
好的佩奇2 天前
Dart 之 JSON
android·flutter·dart