how would I get an instance of navController to gi...
# compose
o
how would I get an instance of navController to give to the Composable in Previews?
t
You don't, that's why the Composable should be stateless. If you need some navigation you can make a lambda callback parameter for the composable which the one on top of it receives and call the
navController
🙂
o
Sure sure good point
but..does it make sense when there’s many uses for it inside a Composable?
really like 6 lambdas as parameters is fine?
s
Yeah, but you could also create your own class to encapsulate all these 6 actions and pass that instead into your composable. Example of NiA doing this where the navigate lambda is inside this custom class
o
sure as a state thing sure
makes sense
what about the composable that hosts the NavHost
that surely gets a navController to it
but then again that’s not going to be a UI thing, thats just going to be the host, so I wont get into the issue of having to provide navController to the Previews, there is no Preview
s
Yeah maybe “State Holder” as a name isn’t quite representative for your use case, those classes can be whatever, in your case it could just be that they have those lambdas all bunched up to make your code be easier to read through.
And yes, exactly as you say above, no problem with previews there since you won’t make one in the first place. That class does need a reference to the NavHost directly since that’s what compose-navigation is asking for in the
NavHost
composable
o
cool cool