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.

相关推荐
mmsx7 小时前
耳切法切带洞多边形:论文能跑通,真实数据为什么崩
android·kotlin
洞见前行7 小时前
你的 Activity 关了,为什么还活着?Android 内存泄漏从发现、定位到线上治理
android·性能优化·leakcanary
Kapaseker9 小时前
Kotlin 又要加语法糖了:这次轮到 if-else
android·kotlin
恋猫de小郭11 小时前
Flutter 3.50 & Dart 3.14 发布,快来看看有什么更新吧
android·前端·flutter
Starry-sky(jing)1 天前
Vidda ADB 开启:R Pro 65 找不到开发者选项的处理与连接验收
android·adb·远程控制·电视·vidda
雨白1 天前
从原理到实践:使用 Kotlin KSP 仿写 ButterKnifeLite
android·kotlin
疯华整猫1 天前
把二十七个 init 从首帧前面搬走:用 Trae 把启动优化钉进 CI
android·性能优化·ai编程
传奇开心果1 天前
【探秘 Jetpack Compose 框架深水区】 第8课:rememberSaveable 的边界在哪里?——Bundle、Saver 与什么值得跨进程存活
android·kotlin