Thread
#compose
    Ash

    Ash

    2 years ago
    Building MVI with Kotlin StateFlow.
    val intentChannel = Channel<Intent>()
    val state: StateFlow<ChannelState>
    // Setup Ambient
    val CurrAppState = ambientOf<ChannelState> { ChannelState.Edit }
    In a Composable want to use the Ambient
    @Composable
    fun Foo(appState: StateFlow<ChannelState>){
       val currState by appState.collectAsState(initial = ChannelState.NotEdit)
       CurrAppState.provides(currState)
    Passing
    currState
    down the tree everything works perferct but if use
    CurrAppState.current
    get the wrong state. Think I am doing something dumb. Thanks for the help.
    Adam Powell

    Adam Powell

    2 years ago
    @Composable
    fun foo(appState: StateFlow<ChannelState>){
      val currState by appState.collectAsState(initial = ChannelState.NotEdit)
      Providers(CurrAppState provides currState) {
        // Content that should see currState
      }
    but really, it's unlikely you want to use ambients for this use case
    Ash

    Ash

    2 years ago
    So just pass it all the way down the tree?
    Adam Powell

    Adam Powell

    2 years ago
    that or use the lexical scope
    a

    alexgrafl

    2 years ago
    Could you elaborate on the “lexical scope”, I could not find anything in the docs
    Zach Klippenstein (he/him) [MOD]

    Zach Klippenstein (he/him) [MOD]

    2 years ago
    Lexical scope is basically the "syntax scope", ie the syntax scope. So in this case I think Adam means instead of passing your state explicitly through your tree as value parameters, have your intermediate composables take
    children
    composable functions (sometimes I've seen these referred to as "slots") and pass lambdas down that close over your state. This is a more extensible approach since it reduces coupling and dependencies on specific types. It's the same approach that composables like Button take, where there's an overload that accepts a composable function for text instead of a string.