Jetpack Compose 中的副作用 [英] SideEffects in Jetpack Compose

查看:103
本文介绍了Jetpack Compose 中的副作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白除了 composable 中的 composition 行为之外,所有其他行为都是副作用.那么,对于 TextField,更改 onValueChange 中的 TextFieldValue 是否有副作用?另外,在 composable 中显示 Toast 消息是否有副作用?

I understood that other than the act of composition in composable, all other actions were side effects. So, in the case of TextField, is changing the TextFieldValue in onValueChange a side effect? Also, is it a side effect to display a toast message in composable?

推荐答案

让我们先了解一些关于函数式编程的术语

Let's first understand few terms w.r.t functional programming

Idempotent - 意味着调用相同的函数 n 次,提供相同的输入,应该产生相同的输出.

Idempotent - means calling the same function n times, providing the same input, should result in same output.

您的函数需要在 compose 世界中是 idempotent,以便重组工作 - 作为 compose 运行时,在初始组合期间 memoizes,如果在重组期间输入函数没有改变(或者说树节点没有更新),compose 运行时将使用相同的 memoized 结果.具有非幂等的函数将导致重构过程中出现意外行为.

Your function needs to be idempotent in compose world, for the recomposition to work - as compose runtime, memoizes during initial composition, and if during recomposition the input of the function have't changed (or say the tree node isn't updated), compose runtime will use the same memoized result. Having a function not being idempotent will lead to unexpected behaviour during recomposition.

Purefunction - 不包含副作用的函数.

Purefunction - Functions that don't contain side-effect.

Side-effect - 任何超出函数范围的动作,以在侧面做一些意想不到的事情.

Side-effect - it's any action that escapes the scope of the function to do something unexpected on the side.

In compose world - side-effect could be a change to the state of the app that happens outside of the scope of the function, things like setting a global variable, updating cache, making a network query, reading from file etc.. 

It makes the function non-deterministic and can lead to race condition and unexpected behavior. 

To generalize, side-effects are unexpected actions happening on the side, out of what callers would expect from the function, and that alter its behavior.

正如我们所说的函数应该是无副作用的,但是在少数情况下我们需要从可组合运行副作用,以处理这些情况,组合运行时提供了不同的 effect-handlers - 这将确保它们根据 compose 生命周期在正确的时间运行.

As we say a function should be side-effect free, however in few scenarios we need to run a side-effect from composable, to handle those cases compose run-time provides different effect-handlers, - which will makes sure, they run at the right time with accordance to the compose lifecycle.

因此,在 TextField 的情况下,正在更改中的 TextFieldValueonValue 改变一个副作用?

So, in the case of TextField, is changing the TextFieldValue in onValueChange a side effect?

它更像是一个单向数据流给我和由于状态是在可组合(可组合的内部存储器)的范围内管理的,所以它不是副作用.

It more looks like an unidirectional data flow to me and the since the state is managed within the scope of composable (internal memory of composable), it's not a side-effect.

另外,在可组合中显示 toast 消息是否有副作用?

Also, is it a side effect to display a toast message in composable?

这可能是一个副作用,如果效果处理不当,考虑到您最终不会在重新组合时调用 Toast.

It can be a side-effect, if the effect is not handled properly, considering, you are not ending up getting the toast called, on ever recomposition.

这篇关于Jetpack Compose 中的副作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆