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.

相关推荐
ktl12 小时前
Android 编译加速/优化 80%:一个文件搞定,零侵入零配置
android
alexhilton1 天前
使用FunctionGemma进行设备端函数调用
android·kotlin·android jetpack
冬奇Lab1 天前
InputManagerService:输入事件分发与ANR机制
android·源码阅读
张小潇1 天前
AOSP15 Input专题InputManager源码分析
android·操作系统
RdoZam1 天前
Android-封装基类Activity\Fragment,从0到1记录
android·kotlin
奥陌陌2 天前
android 打印函数调用堆栈
android
用户985120035832 天前
Compose Navigation 3 深度解析(二):基础用法
android·android jetpack
恋猫de小郭2 天前
Android 官方正式官宣 AI 支持 AppFunctions ,Android 官方 MCP 和系统级 OpenClaw 雏形
android·前端·flutter
黄林晴2 天前
Android 17 Beta 2,隐私这把锁又拧紧了
android
Kapaseker2 天前
研究表明,开发者对Kotlin集合的了解不到 20%
android·kotlin