何时可以在 LazyList 或列的范围内使用可组合项?
我使用 Jetpack Compose 构建了许多项目和组件。我已阅读 StackOverFlow 上的文档和其他教程、网站和帖子。
我不清楚何时允许在另一个可组合项中使用可组合项和可组合函数,特别是使用 LazyColumn
。
我有一些组件,在 LazyColumn
内部使用 Row
或 Column
。其他时候,我尝试创建一个 itemsIndexed
列表,该列表创建一系列 Row
或 Card
,但我收到一条错误 @可组合调用只能在 @Composable 函数的上下文中发生
。
LazyColumn
不被视为 @Composable 函数吗?整个 LazyColumn 位于 @Composable 函数本身内。
我对上下文从可组合环境到不可组合环境的变化感到困惑。
谁能解释一下吗?我在困惑什么?
I have built a number of projects and components with Jetpack Compose. I've read the docs and other tutorials, sites, and posts on StackOverFlow.
I am unclear about when I am allowed to use composables and composable functions inside of another composable, particularly with a LazyColumn
.
I have components where I use a Row
or a Column
inside of a LazyColumn
. Other times I try to create an itemsIndexed
list that creates a series of Row
s or Card
s and I get an error that @Composable invocations can only happen from the context of a @Composable function
.
Isn't the LazyColumn
considered a @Composable function? The entire LazyColumn
is within a @Composable function itself.
I'm confused about where the context changes from a composable environment to a non-composable environment.
Can anyone explain? What am I confusing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如上所述,您需要从用
@Composable
注解的函数调用可组合项,如果您检查 LazyColumn 函数签名,
您将看到内容不是可组合项。另一方面,
items
函数使用itemContent
作为可组合函数,因此您可以调用可组合函数,例如 Column
inside
item 函数。Column
、Row
或Box
等函数的 XScope 函数标记为@Composable
因此,您可以在
Column
、Row
或Box
内调用另一个Composable
。As this says you need to call a Composable from a function that is annotated with
@Composable
If you check LazyColumn function signature
You will see that content is not a Composable. On the other hand
items
function useitemContent
as ComposableBecause of this you can call Composable functions like Column
inside
item function.Functions like
Column
,Row
orBox
have their XScope functions marked with@Composable
And because of this you can call another
Composable
inside aColumn
,Row
orBox
.