Hi <@UHAJKUSTU> With Decompose, do you need to have a component for a screen to be a part of a navig...
x
Hi @Arkadii Ivanov With Decompose, do you need to have a component for a screen to be a part of a navigation? i.e can simple screen exist without any instance attached to it? More context in the ๐Ÿงต
Here, i have a simple screen
IslandScreen
which is just a wrapper around a simple view. However, in order to show this screen alongside with all the other screens, I need to declare an
IslandComponent
.
Copy code
@Composable
fun MainUi(component: MainComponent) {
  Children(..) {
    var numbers by remember { mutableStateOf(listOf(1, 2, 3)) }
    when(val instance: ComponentContext = it.instance){
      is IslandComponent -> IslandUi(instance)
      is ShipComponent -> ShipUi(instance)
      is DockComponent -> DockUi(instance)
    }
  }
}
This is because the
childFactory
of
MainComponent
here, requires you to provide a component
Copy code
val childStack: Value<ChildStack<Config, ComponentContext>> = childStack(
    source = navigation,
    ..
    childFactory = { configuration, componentContext ->
      when(configuration){
        Island -> IslandComponent(componentContext) { .. }

        Ship -> ShipComponent(
          componentContext = componentContext,
          ..
        )

        Dock ->  DockComponent(componentContext = componentContext) { .. }
      }
    }
  )
a
Any entity can be a component, there is no need to pass the child ComponentContext if it's unused.
x
but the
childFactory
would still require to create an instance for
Island
configuration, even though it is not used by the ui - right?
a
Configurations are not used by the UI, typically. Configurations are for the navigation to properly manage lifecycles, and for you to properly create child components. Also, childFactory doesn't create configurations.
๐Ÿ‘ 1
In childFactory you need to create an instance of a component, that you will use in the UI.
x
in the example above, i'm trying to get rid of the
IslandComponent
all-together because this particular screen doesn't have any logic - as it just basically shows a image. However, this won't be possible as
childFactory
will need an instance to be created and stored in the
childStack
- is that correct?
a
You need to return an instance of any type from childFactory. Your IslandComponent holds a callback, which is used by the UI. So it seems reasonable to wrap it with a class. Right? You could just return the callback itself, and then just pass the callback into IslandUi. But I find this confusing.
You can also return Unit, if you don't need anything at all.
x
ah right ๐Ÿ‘ for some reason i assumed it has to return a
ComponentContext
but as you mentioned, it can return
Any
so a Unit makes more sense here
a
In your example, you are returning components wrapping ComponentContext, not ComponentContext itself. So yeah, any type can be returned.
x
thanks arkadii
๐Ÿ‘ 1
a
Btw, I thought you were using Decompose-Router for navigation. Did something change?
x
i still am. I'm also working on a my talk for devfest where i compare and contrast navigation libraries and their apis. Wanted to have set of slides where i showcase decompose by itself without the router-api
a
Got it. Thanks!