A follower asked me this: if we follow the official Navigation 3 guide, a screen needs one function-type parameter for every destination it can go to. How do we avoid that?

The question

Here’s the situation:

Let’s say we have a composable ChatList() with a parameter onChatClick: (Chat) -> Unit:

@Composable
fun ChatList(chats: List<Chat>, onChatClick: (Chat) -> Unit) {
Column(Modifier
.background(WeComposeTheme.colors.background)
.fillMaxSize()) {
WeTopBar("扔信")
LazyColumn(Modifier.background(WeComposeTheme.colors.listItem)) {
itemsIndexed(chats) { index, chat ->
if (index > 0) {
HorizontalDivider(
Modifier.padding(start = 68.dp),
color = WeComposeTheme.colors.divider,
thickness = 0.8f.dp
)
}
ChatListItem(chat, Modifier.clickable { onChatClick(chat) })
}
}
}
}

Passing navigation callbacks in as parameters, instead of passing the back stack into the composable, is exactly what the official Navigation docs recommend.

And here’s the problem:

That feels like too much.

So the question was: how do we avoid this?

The “solution”

If you don’t want too many parameters in your composables, Compose has a great tool for you: CompositionLocal.

You just turn each callback into a CompositionLocal, and provide them where you set up navigation:

val LocalOnChatClick = staticCompositionLocalOf<(Chat) -> Unit> {
error("LocalOnChatClick not provided")
}
val LocalOnSearchClick = staticCompositionLocalOf<() -> Unit> {
error("LocalOnSearchClick not provided")
}
// ...6 more like these
// Where you set up NavDisplay()
entry<ChatListKey> {
CompositionLocalProvider(
LocalOnChatClick provides { chat -> backStack.add(ChatKey(chat.id)) },
LocalOnSearchClick provides { backStack.add(SearchKey) },
// ...6 more
) {
ChatList(chats)
}
}

Then you read them with .current, right where you need them:

@Composable
fun ChatList(chats: List<Chat>) {
val onChatClick = LocalOnChatClick.current
// ...
ChatListItem(chat, Modifier.clickable { onChatClick(chat) })
// ...
}

Since they’re declared at the top level (which is what Compose wants you to do — it’s not an anti-pattern), you can reach them from any composable. Just type LocalXxx.current, and there you go.

And since every LocalXxx is already reachable, you don’t have to pass any of them as parameters:

// Before
@Composable
fun ChatList(
chats: List<Chat>,
onChatClick: (Chat) -> Unit,
onSearchClick: () -> Unit,
// ...6 more
) { /* ... */ }
// After
@Composable
fun ChatList(chats: List<Chat>) { /* ... */ }

Yay! Problem solved.

But wait… what was the “problem” again?

Too many parameters?

Is that really a problem?

The fact

Imagine you’re in a subway station, trying to find the exit to the hospital. It’s a central station, so there are quite a few exits — let’s say 8.

You look at the map on the wall, trying to figure out which exit is which, and it’s confusing.

But would you blame the station for having too many exits? Probably not. Eight exits is confusing, sure. But the station design isn’t what makes it complicated. The area around it is: there’s a lot there, and every exit leads somewhere people need to go. The only thing you could complain about is: why does this neighborhood have to be so popular?

Now let’s pretend the world has magic. OK, I admit that’s silly, but this metaphor is hard to make work any other way.

The station manager decides 8 exits is too many. They seal all 8 doors and put up a sign: to reach an exit, say its magic phrase, and you’ll be there. The station looks clean now. No more confusing signs. But the 8 exits haven’t gone anywhere. You just can’t see them anymore, and you have to already know all 8 phrases to use them. And if you say a phrase somewhere the magic was never set up, it doesn’t work, and you only find out when you try.

I know magic sounds appealing. Who wouldn’t want to say a word and be at the exit? But CompositionLocal doesn’t come with the teleporting. Your code doesn’t get to those 8 screens any faster or any more easily. You still declare 8 Locals and provide all 8 of them. All you get from the magic is a station that looks clean.

Now let’s get back to Compose.

Is a composable with 8 callback parameters pointing to 8 different screens complicated for developers? Yes. But should we try to get rid of them with something like CompositionLocal? Definitely not.

In this case, CompositionLocal doesn’t remove any complexity from the composable. It only hides it from the signature. The screen still leads to 8 places, but you can no longer tell which ones by looking at it; you have to read its whole body. And the 8 callbacks are all still there: they moved to where you provide them, and each one now has a declaration as well. If someone forgets to provide one of them, they find out from a crash at runtime instead of a compile error.

Then what?

OK, so you’re sold, and you won’t use CompositionLocal here.

But then how do we solve the problem?

We don’t.

How do you solve the “too many exits” problem in a station? You don’t, because it was never a problem. Some places in the world are complex, and they’re supposed to be. Just let them be.

Same goes for callback parameters. The main screen of your app is bound to have lots of destinations. Accept that, handle them carefully, and that’s it.

So what about CompositionLocal?

If you’re still thinking about CompositionLocal, you might ask: “If it only hides problems instead of solving them, why does Compose have it at all?”

CompositionLocal isn’t useless. Quite the opposite — it’s super useful. It’s just the wrong tool here.

As for when it’s the right tool, let’s save that for another article.