How to share business logic? Similar to using viewmodel, multiple pages share one viewmodel #914
Unanswered
cjcj125125
asked this question in
Q&A
Replies: 1 comment
-
If you want to share a component between other child components, you have to scope the shared component somewhere at the parent level and then inject it to your child components. Pseudocode: class SharedComponent(componentContext: ComponentContext)
class Component1(componentContext: ComponentContext, sharedComponent: SharedComponent)
class Component2(componentContext: ComponentContext, sharedComponent: SharedComponent)
class ParentComponent(componentContext: ComponentContext) {
private val sharedComponent = SharedComponent(childContext("shared-component"))
val stack = childStack(...) { cfg, ctx ->
when (cfg) {
is Config.Config1 -> Component1(ctx, sharedComponent)
is Config.Config2 -> Component2(ctx, sharedComponent)
}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
When using viewmodel before, I could use the activity's viewmodel in the fragment. So when using decompose, how can multiple pages share a component?
Beta Was this translation helpful? Give feedback.
All reactions